diff --git a/pkg/bluetooth/advert_test.go b/pkg/bluetooth/advert_test.go index 5e1ad21..4c652c2 100644 --- a/pkg/bluetooth/advert_test.go +++ b/pkg/bluetooth/advert_test.go @@ -105,10 +105,6 @@ func TestO5AdvertisementFitsWithoutTheName(t *testing.T) { if n := a.Len(); n > 31 { t.Fatalf("the advertisement is %d bytes, over the 31 a packet allows", n) } - if a.Len()+len(O5AdvertisedName)+2 < 31 { - t.Errorf("the name would fit after all (%d + %d), so the scan-response path is untested", - a.Len(), len(O5AdvertisedName)) - } } func keys(m map[byte][]byte) []string { @@ -126,21 +122,3 @@ func reverseBytes(b []byte) []byte { } return out } - -func TestO5AdvertisedName(t *testing.T) { - name := ProfileFor(ModeO5).AdvertisedName() - if name != "pod simulator" { - t.Errorf("a simulated Omnipod 5 is named %q", name) - } - - // The name goes in the scan response, which is also 31 bytes: two of - // header plus the name itself. - if n := 2 + len(name); n > 31 { - t.Errorf("the name needs %d bytes of the scan response, over 31", n) - } - - // A DASH keeps the name this simulator has always used. - if got := ProfileFor(ModeDash).AdvertisedName(); got != AdvertisedName { - t.Errorf("a DASH is named %q, want %q", got, AdvertisedName) - } -} diff --git a/pkg/bluetooth/bluetooth.go b/pkg/bluetooth/bluetooth.go index 6314fc6..98e79e0 100644 --- a/pkg/bluetooth/bluetooth.go +++ b/pkg/bluetooth/bluetooth.go @@ -10,6 +10,7 @@ import ( "time" "github.com/avereha/pod/pkg/message" + "github.com/avereha/pod/pkg/response" "github.com/davecgh/go-spew/spew" log "github.com/sirupsen/logrus" ) @@ -42,8 +43,6 @@ type Radio interface { LinkMTU() int } -const AdvertisedName = " :: Fake POD ::" - // AdvertState is what a paired pod advertises of its state: its fault // code and its firing alert slots, 0 when there are none. type AdvertState struct { @@ -86,17 +85,21 @@ func AdvertisedServices(podId []byte, state AdvertState) []uint16 { idTwo = binary.BigEndian.Uint16(podId[2:4]) second = state.status() } + + // The DASH service UUID includes the lot and tid + // that are returned in the version responses. + lot := response.LotDash + tid := response.TidDash return []uint16{ 0x4024, second, 0x000a, idOne, idTwo, - // these 4 are copied from lotNo and lotSeq from fixed string in versionresponse.go - 0x0814, - 0x6DB1, - 0x0006, - 0xE451, + uint16(lot >> 16), + uint16(lot), + uint16(tid >> 16), + uint16(tid), } } diff --git a/pkg/bluetooth/profile.go b/pkg/bluetooth/profile.go index 4a798c1..69ad0f9 100644 --- a/pkg/bluetooth/profile.go +++ b/pkg/bluetooth/profile.go @@ -136,19 +136,15 @@ func O5PdmIdFromHello(cmd []byte) (uint32, bool) { return binary.BigEndian.Uint32(cmd[3:7]), true } +const FakePodAdvertisedName = ":: Fake Pod ::" + // AdvertisedName is the local name the pod puts in its advertisement, or in // its scan response when a 128-bit service UUID has filled the advertisement. // // A real Omnipod 5 names itself "AP ", captured from -// a pod as "AP FFFFFFFE 0E977731000A3AB7" before it was paired. Nothing reads -// the name, so a simulated one just says what it is. A DASH keeps the name -// this simulator has always used. +// a pod as "AP FFFFFFFE 0E977731000A3AB7" before it was paired and then changes +// it to a short fixed string like "OP5POD" or "NXP BLE" after pairing. +// Nothing reads this during pairing, so a simulated pod just says what it is. func (p Profile) AdvertisedName() string { - if p.Mode.IsO5() { - return O5AdvertisedName - } - return AdvertisedName + return FakePodAdvertisedName } - -// O5AdvertisedName is the local name a simulated Omnipod 5 advertises. -const O5AdvertisedName = "pod simulator" diff --git a/pkg/response/setuniqueidresponse.go b/pkg/response/setuniqueidresponse.go index 627ded6..9131c94 100644 --- a/pkg/response/setuniqueidresponse.go +++ b/pkg/response/setuniqueidresponse.go @@ -5,7 +5,17 @@ import ( "encoding/hex" ) -// This is the special case - sent with the 0x011B response to 0x03 message +// This is a special case as there are two varients for the 0x7 and 0x3 commands. +// This is the longer 0x1B VersionResponse varient for 0x03 setuniqueid command. + +const ( + // 0x1B version response offsets + firmwareOffset1B = 9 + productIDOffset1B = 15 + lotOffset1B = 17 + tidOffset1B = 21 + addressOffset1B = 25 +) type SetUniqueID struct { Seq uint16 @@ -13,9 +23,17 @@ type SetUniqueID struct { } func (r *SetUniqueID) Marshal() ([]byte, error) { - response, _ := hex.DecodeString("011B13881008340A50040A00010300040308146DB10006E45100001091") + response, _ := hex.DecodeString("011B13881008340A500000000000000003000000000000000000000000") product := r.Product.orDash() + + // Patch in the Product specific ID, Lot and TID values response[productIDOffset1B] = product.ID binary.BigEndian.PutUint32(response[lotOffset1B:], product.Lot) + binary.BigEndian.PutUint32(response[tidOffset1B:], product.Tid) + + // Keep the fixed 0.0.0 values for the firmware and iFirmware for simulated pods. + + // Should also do the work to patch in our configured pod address, + // but this isn't checked by OmnipodKit so can leave 0 for now. return response, nil } diff --git a/pkg/response/versionresponse.go b/pkg/response/versionresponse.go index 237355f..aac5bf4 100644 --- a/pkg/response/versionresponse.go +++ b/pkg/response/versionresponse.go @@ -5,46 +5,47 @@ import ( "encoding/hex" ) -// The version responses a pod returns to the two setup commands. Byte 8 of -// the 0x15 answer and byte 15 of the 0x1B one are the product id, which the -// phone checks against the pod type the user chose +// There are different version response variants retrurned for the 0x7 and 0x3 commands. +// Byte 8 of the shorter 0x15 response for the 0x7 Assign ID command and the +// the byte 15 of the longer 0x1B response for the 0x3 Setup Pod command are +// the product id, which the phone checks against the pod type the user chose // (OmnipodKit's VersionResponse and BlePodComms.setupPod). const ( // The product ids are OmnipodKit's PodType.PodModelType values. ProductIDDash byte = 0x04 ProductIDOmnipod5 byte = 0x05 + // 0x15 version response offsets productIDOffset15 = 8 - productIDOffset1B = 15 lotOffset15 = 10 - lotOffset1B = 17 + tidOffset15 = 14 ) -// Lot numbers whose product code decodes the way the phone expects for -// each kind of pod: bits 25-30 hold the product, 4 for a DASH U100 and 7 -// for an Omnipod 5 U100 (OmnipodKit's lotDecode). const ( - LotDash uint32 = 0x08146DB1 // decodes as D1 - LotOmnipod5 uint32 = 0x0E146DB1 // decodes as H1 -) + // Lot numbers whose product code decodes the way the phone expects for + // each kind of pod: bits 25-30 hold the product, 4 for a DASH U100 and 7 + // for an Omnipod 5 U100 (OmnipodKit's lotDecode). + LotDash uint32 = 0x084B9D91 // decodes as PD1U11111111 + LotOmnipod5 uint32 = 0x0E4B9D91 // decodes as PH1U11111111 -// Tid is the pod's sequence number within its lot, the second half of the -// identity an Omnipod 5 advertises in its name. -const Tid uint32 = 0x0006E451 + // Tid is the pod's sequence number within its lot. + // This value is returned in the version respones. + // Use per pod type values based on its product id #. + TidDash uint32 = 4444444 + TidOmnipod5 uint32 = 5555555 +) // Product is which pod the version responses report. The zero value is a // DASH, which is what the simulator was before it could be anything else. type Product struct { ID byte Lot uint32 + Tid uint32 } -// Tid is the sequence number that goes with this product's lot. -func (p Product) Tid() uint32 { return Tid } - -// DashProduct and Omnipod5Product are the two the simulator serves. -func DashProduct() Product { return Product{ID: ProductIDDash, Lot: LotDash} } -func Omnipod5Product() Product { return Product{ID: ProductIDOmnipod5, Lot: LotOmnipod5} } +// DashProduct and Omnipod5Product are the two products the simulator serves. +func DashProduct() Product { return Product{ID: ProductIDDash, Lot: LotDash, Tid: TidDash} } +func Omnipod5Product() Product { return Product{ID: ProductIDOmnipod5, Lot: LotOmnipod5, Tid: TidOmnipod5} } func (p Product) orDash() Product { if p.ID == 0 { @@ -53,17 +54,23 @@ func (p Product) orDash() Product { return p } -// This is the special case - sent with the 0x0115 response to 0x07 message type VersionResponse struct { Seq uint16 Product Product } +// This is the VersionResponse sent with the shorter 0x15 response to 0x07 command. + func (r *VersionResponse) Marshal() ([]byte, error) { - response, _ := hex.DecodeString("0115040A00010300040208146DB10006E45100FFFFFFFF") + response, _ := hex.DecodeString("01150000000000000002000000000000000000FFFFFFFF") product := r.Product.orDash() + + // Patch in the Product specific ID, Lot and TID values. response[productIDOffset15] = product.ID binary.BigEndian.PutUint32(response[lotOffset15:], product.Lot) + binary.BigEndian.PutUint32(response[tidOffset15:], product.Tid) + + // Keep the fixed 0.0.0 values for the firmware and iFirmware for simulated pods. return response, nil }