Skip to content

Commit 76a0001

Browse files
authored
Merge pull request #251 from xuliqun25/iot_link
Iot link
2 parents 8c72f43 + c7eb7b0 commit 76a0001

File tree

6 files changed

+487
-278
lines changed

6 files changed

+487
-278
lines changed

iot_link/oc/oc_mqtt/oc_mqtt_tiny/oc_mqtt_tiny.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,17 +362,23 @@ static int daemon_cmd_post(en_oc_mqtt_daemon_cmd cmd, void *arg)
362362
static int config_parameter_release(oc_mqtt_tiny_cb_t *cb)
363363
{
364364

365-
osal_free(cb->config.id);
365+
if(cb->config.id != NULL)
366+
osal_free(cb->config.id);
366367

367-
osal_free(cb->config.pwd);
368+
if(cb->config.pwd != NULL)
369+
osal_free(cb->config.pwd);
368370

369-
osal_free(cb->config.server_addr);
371+
if(cb->config.server_addr != NULL)
372+
osal_free(cb->config.server_addr);
370373

371-
osal_free(cb->config.server_port);
374+
if(cb->config.server_port != NULL)
375+
osal_free(cb->config.server_port);
372376

373-
osal_free(cb->bs_cb.hubserver_addr);
377+
if(cb->bs_cb.hubserver_addr != NULL)
378+
osal_free(cb->bs_cb.hubserver_addr);
374379

375-
osal_free(cb->bs_cb.hubserver_port);
380+
if(cb->bs_cb.hubserver_port != NULL)
381+
osal_free(cb->bs_cb.hubserver_port);
376382

377383
memset(&cb->config,0,sizeof(oc_mqtt_config_t));
378384

@@ -425,7 +431,6 @@ static int config_parameter_clone(oc_mqtt_tiny_cb_t *cb,oc_mqtt_config_t *config
425431
cb->config.server_addr = osal_strdup(config->server_addr);
426432
cb->config.server_port = osal_strdup(config->server_port);
427433

428-
429434
if((NULL == cb->config.id) || (NULL == cb->config.pwd) ||\
430435
(NULL == cb->config.server_addr) || (NULL == cb->config.server_port))
431436
{
@@ -897,6 +902,11 @@ static int tiny_config(oc_mqtt_config_t *config)
897902
int ret = en_oc_mqtt_err_parafmt;
898903
if(NULL != config)
899904
{
905+
if((NULL == config->id) || (NULL == config->pwd) ||\
906+
(NULL == config->server_addr) || (NULL == config->server_port))
907+
{
908+
return ret;
909+
}
900910
ret = daemon_cmd_post(en_oc_mqtt_daemon_cmd_connect,config);
901911
}
902912
return ret;

test/c_dependcy/test_case_oc_mqtt.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -592,33 +592,57 @@ static int ts_oc_mqtt_config(char *message, int len)
592592
{
593593
osal_free(pparas->id);
594594
}
595-
pparas->id = osal_malloc(strlen(pchTmp)+1);
596-
memcpy(pparas->id, pchTmp, strlen(pchTmp));
597-
pparas->id[strlen(pchTmp)] = '\0';
598-
printf("120--id is %s\n",pparas->id);
595+
if((!memcmp(pchTmp, "NULL", strlen(pchTmp))) ||
596+
(!memcmp(pchTmp, "null", strlen(pchTmp))))
597+
{
598+
pparas->id = NULL;
599+
}
600+
else
601+
{
602+
pparas->id = osal_malloc(strlen(pchTmp)+1);
603+
memcpy(pparas->id, pchTmp, strlen(pchTmp));
604+
pparas->id[strlen(pchTmp)] = '\0';
605+
printf("120--id is %s\n",pparas->id);
606+
}
599607

600608
pchTmp = strtok_r(NULL, "|", &pchStrTmpIn);
601609
if(pparas->passwd != NULL)
602610
{
603611
osal_free(pparas->passwd);
604612
}
605-
pparas->passwd = osal_malloc(strlen(pchTmp)+1);
606-
memcpy(pparas->passwd, pchTmp, strlen(pchTmp));
607-
pparas->passwd[strlen(pchTmp)] = '\0';
613+
if((!memcmp(pchTmp, "NULL", strlen(pchTmp))) ||
614+
(!memcmp(pchTmp, "null", strlen(pchTmp))))
615+
{
616+
pparas->passwd = NULL;
617+
}
618+
else
619+
{
620+
pparas->passwd = osal_malloc(strlen(pchTmp)+1);
621+
memcpy(pparas->passwd, pchTmp, strlen(pchTmp));
622+
pparas->passwd[strlen(pchTmp)] = '\0';
623+
printf("140--passwd is %s\n",pparas->passwd);
624+
}
608625

609-
printf("140--passwd is %s\n",pparas->passwd);
626+
610627

611628
pchTmp = strtok_r(NULL, "|", &pchStrTmpIn);
612629

613630
if(pparas->cbname != NULL)
614631
{
615632
osal_free(pparas->cbname);
616633
}
617-
pparas->cbname = osal_malloc(strlen(pchTmp)+1);
618-
memcpy(pparas->cbname, pchTmp, strlen(pchTmp));
619-
pparas->cbname[strlen(pchTmp)] = '\0';
620-
printf("125--cbname is %s\n",pparas->cbname);
621-
634+
if((!memcmp(pchTmp, "NULL", strlen(pchTmp))) ||
635+
(!memcmp(pchTmp, "null", strlen(pchTmp))))
636+
{
637+
pparas->cbname = NULL;
638+
}
639+
else
640+
{
641+
pparas->cbname = osal_malloc(strlen(pchTmp)+1);
642+
memcpy(pparas->cbname, pchTmp, strlen(pchTmp));
643+
pparas->cbname[strlen(pchTmp)] = '\0';
644+
printf("125--cbname is %s\n",pparas->cbname);
645+
}
622646

623647

624648
config.boot_mode = pparas->boot_mode;

test/python/test_linux_oc_mqtt.py

Lines changed: 31 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ def test_linux_oc_mqtt_register():
5858
assert(result.test_id == mqtt_testid.TEST_OC_MQTT_REGISTER)
5959
assert(result.ret_code == 0)
6060

61+
62+
# typedef enum
63+
# {
64+
# en_oc_mqtt_err_ok = 0, ///< this means the status ok
65+
# en_oc_mqtt_err_parafmt, ///< this means the parameter err format
66+
# en_oc_mqtt_err_network, ///< this means the network wrong status
67+
# en_oc_mqtt_err_conversion, ///< this means the mqtt version err
68+
# en_oc_mqtt_err_conclientid, ///< this means the client id is err
69+
# en_oc_mqtt_err_conserver, ///< this means the server refused the service for some reason(likely the id and pwd)
70+
# en_oc_mqtt_err_conuserpwd, ///< bad user name or passwd
71+
# en_oc_mqtt_err_conclient, ///< the client id /user/pwd is right, but does not allowed
72+
# en_oc_mqtt_err_subscribe, ///< this means subscribe the topic failed
73+
# en_oc_mqtt_err_publish, ///< this means subscribe the topic failed
74+
# en_oc_mqtt_err_configured, ///< this means we has configured, please deconfigured it and then do configure again
75+
# en_oc_mqtt_err_noconfigured, ///< this means we have not configure it yet,so could not connect
76+
# en_oc_mqtt_err_noconected, ///< this means the connection has not been built, so you could not send data
77+
# en_oc_mqtt_err_gethubaddrtimeout, ///< this means get the hub address timeout
78+
# en_oc_mqtt_err_sysmem, ///< this means the system memory is not enough
79+
# en_oc_mqtt_err_system, ///< this means that the system porting may have some problem,maybe not install yet
80+
# en_oc_mqtt_err_last,
81+
# }en_oc_mqtt_err_code_t;
82+
6183
def test_linux_oc_mqtt_config_invalid():
6284
fname = sys._getframe().f_code.co_name
6385
#invalid server ip
@@ -68,7 +90,7 @@ def test_linux_oc_mqtt_config_invalid():
6890
CB_NAME)
6991
assert(result)
7092
assert(result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
71-
assert(result.ret_code == -1)
93+
assert(result.ret_code == 1) #en_oc_mqtt_err_parafmt
7294

7395
# invalid server port
7496
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_CONFIG, fname,
@@ -78,7 +100,7 @@ def test_linux_oc_mqtt_config_invalid():
78100
CB_NAME)
79101
assert (result)
80102
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
81-
assert (result.ret_code == -1)
103+
assert (result.ret_code == 1) #en_oc_mqtt_err_parafmt
82104

83105
# invalid security type
84106
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_CONFIG, fname,
@@ -88,7 +110,7 @@ def test_linux_oc_mqtt_config_invalid():
88110
CB_NAME)
89111
assert (result)
90112
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
91-
assert (result.ret_code == -1)
113+
assert (result.ret_code == 1) #en_oc_mqtt_err_parafmt
92114

93115

94116
# invalid ep name
@@ -99,7 +121,7 @@ def test_linux_oc_mqtt_config_invalid():
99121
CB_NAME)
100122
assert (result)
101123
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
102-
assert (result.ret_code == -1)
124+
assert (result.ret_code == 1) #en_oc_mqtt_err_parafmt
103125

104126
# invalid passwd
105127
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_CONFIG, fname,
@@ -109,7 +131,7 @@ def test_linux_oc_mqtt_config_invalid():
109131
CB_NAME)
110132
assert (result)
111133
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
112-
assert (result.ret_code == -1)
134+
assert (result.ret_code == 1) #en_oc_mqtt_err_parafmt
113135

114136

115137

@@ -260,126 +282,6 @@ def test_linux_oc_mqtt_deconfig_static():
260282
# assert (status == "OFFLINE")
261283
print(status)
262284

263-
264-
def test_linux_oc_mqtt_config_bs():
265-
fname = sys._getframe().f_code.co_name
266-
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_CONFIG, fname,
267-
en_oc_boot_strap_mode_client_initialize, LIFE_TIME,
268-
BS_SERVER_IP4, BS_SERVER_PORT, SEC_TYPE,
269-
mqtt_device_info.BS_MQTT_EP_USER, mqtt_device_info.BS_MQTT_EP_PASSWD,
270-
CB_NAME)
271-
assert(result)
272-
assert(result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
273-
assert(result.ret_code == 0)
274-
time.sleep(2)
275-
# oc_mqtt_device_status_jduge_b(mqtt_device_info.BS_MQTT_DEVICEID, "ONLINE")
276-
277-
def test_linux_oc_mqtt_json_fmt_req_bs():
278-
fname = sys._getframe().f_code.co_name
279-
#must send 4 items here
280-
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_JSON_FMT_REQ, fname,
281-
"LED", 0, #LED is serviceId,0 means en_oc_mqtt_has_more_no
282-
"LED1", 0, 1,#LED1 is item name, 0 means en_key_value_type_int, 1 means value
283-
"LED2", 0, 2,
284-
"LED3", 0, 35,
285-
"LED4", 0, 78
286-
)
287-
assert (result)
288-
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_JSON_FMT_REQ)
289-
assert (result.ret_code == 1)
290-
print("----------------------------------------------")
291-
print("hhhh")
292-
print(json.loads(result.data).get("data")[0])
293-
print("----------------------------------------------")
294-
# typedef enum
295-
# {
296-
# en_oc_mqtt_has_more_no = 0,
297-
# en_oc_mqtt_has_more_yes = 1,
298-
# }en_oc_mqtt_has_more;
299-
# typedef enum
300-
# {
301-
# en_key_value_type_int = 0,
302-
# en_key_value_type_string,
303-
# en_key_value_type_array,
304-
# }en_value_type;
305-
# parase received json data
306-
# {
307-
# "msgType": "deviceReq",
308-
# "hasMore": 0,
309-
# "data": [{
310-
# "serviceId": "LED",
311-
# "serviceData": {
312-
# "LED1": 1,
313-
# "LED2": 2,
314-
# "LED2": 35,
315-
# "LED2": 78
316-
# }
317-
# }]
318-
# }
319-
320-
assert (json.loads(result.data).get("msgType") == "deviceReq")
321-
assert (json.loads(result.data).get("hasMore") == 0)
322-
data = json.loads(result.data).get("data")[0]
323-
assert (data["serviceId"] == "LED")
324-
print("+++++++++++++++++++++++++++++++++++++++++++")
325-
print(result.data)
326-
print(data)
327-
print(data["serviceData"])
328-
print("+++++++++++++++++++++++++++++++++++++++++++")
329-
assert (data.get("serviceData").get("LED1") == 1)
330-
assert (data.get("serviceData").get("LED2") == 2)
331-
assert (data.get("serviceData").get("LED3") == 35)
332-
assert (data.get("serviceData").get("LED4") == 78)
333-
334-
335-
def test_linux_oc_mqtt_json_fmt_res_bs():
336-
fname = sys._getframe().f_code.co_name
337-
# must send 1 items here
338-
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_JSON_FMT_RES, fname,
339-
0, 0, 1, # 0 means en_oc_mqtt_has_more_no, second means no error, 1 means mid
340-
"body_para", 1, "body_para" # body_para is item name, second means string type, third means value
341-
)
342-
print(result)
343-
assert (result)
344-
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_JSON_FMT_RES)
345-
assert (result.ret_code == 1)
346-
print(result.data)
347-
348-
# {
349-
# "msgType": "deviceRsp",
350-
# "mid": 1,
351-
# "errcode": 0,
352-
# "hasMore": 0,
353-
# "body": {
354-
# "body_para": "body_para"
355-
# }
356-
# }
357-
358-
assert (json.loads(result.data).get("msgType") == "deviceRsp")
359-
assert (json.loads(result.data).get("mid") == 1)
360-
assert (json.loads(result.data).get("errcode") == 0)
361-
assert (json.loads(result.data).get("hasMore") == 0)
362-
assert (json.loads(result.data).get("body").get("body_para") == "body_para")
363-
364-
365-
def test_linux_oc_mqtt_publish_bs():
366-
fname = sys._getframe().f_code.co_name
367-
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_PUBLISH, fname)
368-
assert (result)
369-
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_PUBLISH)
370-
assert (result.ret_code == 0)
371-
372-
def test_linux_oc_mqtt_deconfig_bs():
373-
fname = sys._getframe().f_code.co_name
374-
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_DECONFIG, fname)
375-
assert (result)
376-
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_DECONFIG)
377-
assert (result.ret_code == 0)
378-
time.sleep(15)
379-
# oc_mqtt_device_status_jduge_b(mqtt_device_info.BS_MQTT_DEVICEID, "OFFLINE")
380-
381-
382-
383285
def test_linux_oc_mqtt_deinit():
384286
fname = sys._getframe().f_code.co_name
385287
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_DEINIT, fname)
@@ -394,16 +296,11 @@ def test_linux_oc_mqtt_deinit():
394296
print("hello world")
395297
test_linux_oc_mqtt_init()
396298
test_linux_oc_mqtt_register()
397-
# test_linux_oc_mqtt_config_invalid()
299+
test_linux_oc_mqtt_config_invalid()
398300
test_linux_oc_mqtt_config_static()
399-
# test_linux_oc_mqtt_json_fmt_req_static()
400-
# test_linux_oc_mqtt_json_fmt_res_static()
401-
# test_linux_oc_mqtt_report_static()
301+
test_linux_oc_mqtt_json_fmt_req_static()
302+
test_linux_oc_mqtt_json_fmt_res_static()
303+
test_linux_oc_mqtt_publish_static()
402304
test_linux_oc_mqtt_sendcmd_static()
403305
test_linux_oc_mqtt_deconfig_static()
404-
# test_linux_oc_mqtt_config_bs()
405-
# test_linux_oc_mqtt_json_fmt_req_bs()
406-
# test_linux_oc_mqtt_json_fmt_res_bs()
407-
# test_linux_oc_mqtt_report_bs()
408-
# test_linux_oc_mqtt_deconfig_bs()
409306
test_linux_oc_mqtt_deinit()

0 commit comments

Comments
 (0)