/* Package for parsing devicePrs byte stream to struct. */ package parser import ( "bytes" "encoding/binary" "fmt" "time" //"encoding/hex" "strconv" "sort" "github.com/ipsusila/opt" "../model" ) type ruptelaTchHeader struct { Length uint16 IMEI uint64 CommandID byte PortID byte Reserved uint16 Timestamp uint32 } type DeviceRecordHeader struct { Imei uint64 Tstamp time.Time NumRec uint16 DeviceRecords []DeviceRecord } type DeviceRecord struct { Tstamp time.Time TstampInt int64 Imei uint64 Longitude float64 Latitude float64 Altitude uint16 Angle uint16 Satelites uint16 Speed uint16 TagDevices map[string]TagDevice } type TagDevice struct { TagName string TagId string TagDataType string TagVal string } // ByTstamp implements sort.Interface for []DeviceRecord based on // the TstampInt field. type ByTstamp []DeviceRecord func (a ByTstamp) Len() int { return len(a) } func (a ByTstamp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByTstamp) Less(i, j int) bool { return a[i].TstampInt < a[j].TstampInt } /* Verify that content of the byte stream match defined format */ func verifyStream(data []byte, minSize int, maxSize int, tagOption *opt.Options) (imei uint64, cmd byte, rec DeviceRecordHeader, err error) { dlen := len(data) if dlen < minSize { return 0, 0,rec, fmt.Errorf("Packet size is too small (< %d)", dlen) } else if dlen > maxSize { return 0, 0,rec, fmt.Errorf("Packet size is greater than maximum allowed size (%d)", dlen) } //Extract packet size sz := int(binary.BigEndian.Uint16(data[:2])) if sz != (dlen - 4) { return 0, 0,rec, fmt.Errorf("Size field mismatched (!%d)", dlen) } //extract IMEI imei = binary.BigEndian.Uint64(data[2:10]) //extract and verify CRC crc := binary.BigEndian.Uint16(data[dlen-2:]) ccrc := crc16CCITT(data[2 : dlen-2]) if crc != ccrc { return 0, 0,rec, fmt.Errorf("Invalid Ruptela packet (CRC-16 does not match)") } //parse data rec = parseData(data,imei,tagOption) //extract command cmd = data[10] fmt.Printf("cmd %v\n",cmd) return imei, cmd, rec, nil } /* Parse byte stream and convert to standard record format */ func parseData(data []byte, imei uint64, tagOption *opt.Options) (rec DeviceRecordHeader) { //allocate records rec.Tstamp = time.Now() rec.Imei = imei tagOption_ := tagOption.GetObjectArray("tags") lengthTags := len(tagOption_) tags := make(map[string]TagDevice) for i:=0; i maxSize { return rec, fmt.Errorf("Packet size is greater than maximum allowed size (%d)", dataLen) } //create buffer buf := bytes.NewBuffer(data) //read packet header part pktHdr := &ruptelaTchHeader{} err := binary.Read(buf, binary.BigEndian, pktHdr) if err != nil { return rec, fmt.Errorf("Failed to read packet header (%v)", err) } //2+8+1+1+2+4 (len, imei, cmd, port, reserved, timestamp) idx := 2 + 8 + 1 + 1 + 2 + 4 rec.IMEI = pktHdr.IMEI rec.PortID = pktHdr.PortID rec.Timestamp = time.Unix(int64(pktHdr.Timestamp), 0) rec.Data = data[idx:(dataLen - 2)] return rec, nil } // Calculate CRC16 with CCITT algorithm // Based on C/C++ code in DevicePrs protocol documents // CRC calculation omits Length(2-bytes) and CRC itself (2-bytes) func crc16CCITT(data []byte) uint16 { //-------------------------------------------------------------------- var ucBit, ucCarry uint16 //-------------------------------------------------------------------- var usPoly uint16 = 0x8408 //reversed 0x1021 var usCRC uint16 //initialized as zero //-------------------------------------------------------------------- for _, d := range data { usCRC ^= uint16(d) for ucBit = 0; ucBit < 8; ucBit++ { ucCarry = usCRC & 1 usCRC >>= 1 if ucCarry != 0 { usCRC ^= usPoly } } } //-------------------------------------------------------------------- return usCRC //-------------------------------------------------------------------- }