Device Discovery and Query
1. Gateway discovery (unencrypted)
To discover gateway in Lan, multicast to (ip: 224.0.0.50, peer_port: 4321). Gateways in Lan will reply their IP status after receiving Whois command:
- PC multicast -> Gateway:
{"cmd":"whois"}
- Gateway unicast->PC:
{"cmd":"iam","ip" : "192.168.0.42","port" : "9898","model" : "gateway",.....}
2. Encryption mechanism
Using key encryption, the default is not encrypted. If the user wants to restrict others to access the gateway and sub-devices, an encription key can be set on the Xiaomi smarthome APP for each gateway (using AES-CBC 128 encryption, the app issued a random 16-byte length of the string key). If there is no encription key configured (i.e. the key is set to the empty string ""), then everyone has permission to control the gateway and its sub-devices.
Notes: The AES-CBC 128 initial vector is defined as: unsigned char const AES_KEY_IV[16] = {0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58, 0x56, 0x2e};
3. Get of sub device ID
Unicast a command to the gateway at udp 9898 port. The gateway replies in unicast mode a list devices in the gateway (the gateway returns the device ID of the sub-device)
- PC->gateway:
{"cmd" : "get_id_list"}
- gateway->PC:
{"cmd" : "get_id_list_ack","sid":"1022780","token":"1234567890abcdef","data":"[\"sid1\",\"sid2\",\"sid3\"]"}
Here “sid” is the gateway did,“token” is a random string generated by the gateway for user to generate write "key"
4. Sub-device status report
Sub devices multicast status to (ip: 224.0.0.50, port: 9898). When the status of a sub device changes, it will report the status. E.g. (Not important.)
gateway->PC:{"cmd":"report","model":"magnet","sid":"89234324","short_id":4343,"data":"{\"status\":\"open\"}"}