Skip to content

Commit

Permalink
chage TLV value type uri to string to make it more common.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruizeng committed Dec 23, 2015
1 parent 8719c45 commit bea3f50
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
12 changes: 6 additions & 6 deletions pkg/tlv/tlv.go
Expand Up @@ -20,7 +20,7 @@ const (
TLV_UINT32 = 9
TLV_UINT64 = 10
TLV_BYTES = 11
TLV_URI = 12
TLV_STRING = 12
TLV_BOOL = 13
)

Expand Down Expand Up @@ -78,7 +78,7 @@ func (tlv *TLV) Length() int {
case TLV_BYTES:
length = int(ByteToUint16(tlv.Value[0:2]))
length += 2
case TLV_URI:
case TLV_STRING:
length = int(ByteToUint16(tlv.Value[0:2]))
length += 2
default:
Expand Down Expand Up @@ -139,7 +139,7 @@ func (tlv *TLV) FromBinary(r io.Reader) error {
tlv.Value = make([]byte, length+2)
copy(tlv.Value[0:2], Uint16ToByte(length))
binary.Read(r, binary.BigEndian, tlv.Value[2:])
case TLV_URI:
case TLV_STRING:
binary.Read(r, binary.BigEndian, &length)
tlv.Value = make([]byte, length+2)
copy(tlv.Value[0:2], Uint16ToByte(length))
Expand Down Expand Up @@ -202,7 +202,7 @@ func MakeTLV(a interface{}) (*TLV, error) {
binary.Write(buf, binary.BigEndian, length)
binary.Write(buf, binary.BigEndian, a.([]byte))
case string:
tag = TLV_URI
tag = TLV_STRING
length = uint16(len(a.(string)))
binary.Write(buf, binary.BigEndian, length)
binary.Write(buf, binary.BigEndian, []byte(a.(string)))
Expand Down Expand Up @@ -277,7 +277,7 @@ func ReadTLV(tlv *TLV) (interface{}, error) {
retvar := make([]byte, length)
err = binary.Read(buffer, binary.BigEndian, &retvar)
return retvar, err
case TLV_URI:
case TLV_STRING:
err := binary.Read(buffer, binary.BigEndian, &length)
if err != nil {
return string([]byte{}), err
Expand Down Expand Up @@ -338,7 +338,7 @@ func CastTLV(value interface{}, valueType int32) interface{} {
return uint64(value.(float64))
case TLV_BYTES:
return []byte(value.(string))
case TLV_URI:
case TLV_STRING:
return value.(string)
default:
return nil
Expand Down
2 changes: 1 addition & 1 deletion services/mqttaccess/access.go
Expand Up @@ -40,7 +40,7 @@ func (a *Access) SetStatus(args rpcs.ArgsSetStatus, reply *rpcs.ReplySetStatus)
if err != nil {
return err
}
return a.MqttBroker.SendMessageToDevice(args.DeviceId, "d", msg, defaultTimeoutSecond*time.Second)
return a.MqttBroker.SendMessageToDevice(args.DeviceId, "s", msg, defaultTimeoutSecond*time.Second)
}

func (a *Access) GetStatus(args rpcs.ArgsGetStatus, reply *rpcs.ReplyGetStatus) error {
Expand Down
4 changes: 3 additions & 1 deletion services/mqttaccess/mqtt_provider.go
Expand Up @@ -60,7 +60,8 @@ func (mp *MQTTProvider) OnDeviceHeartBeat(deviceid uint64) error {
func (mp *MQTTProvider) OnDeviceMessage(deviceid uint64, msgtype string, message []byte) {
server.Log.Infof("device {%v} message {%v} : %x", deviceid, msgtype, message)
switch msgtype {
case "d":
case "s":
// it's a status
data := &protocol.Data{}
err := data.UnMarshal(message)
if err != nil {
Expand All @@ -87,6 +88,7 @@ func (mp *MQTTProvider) OnDeviceMessage(deviceid uint64, msgtype string, message
return
}
case "e":
// it's an event report
event := &protocol.Event{}
err := event.UnMarshal(message)
if err != nil {
Expand Down

0 comments on commit bea3f50

Please sign in to comment.