Skip to content

Commit

Permalink
Merge pull request #251 from xuliqun25/iot_link
Browse files Browse the repository at this point in the history
Iot link
  • Loading branch information
cruise2018 authored Dec 4, 2019
2 parents 8c72f43 + c7eb7b0 commit 76a0001
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 278 deletions.
24 changes: 17 additions & 7 deletions iot_link/oc/oc_mqtt/oc_mqtt_tiny/oc_mqtt_tiny.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,23 @@ static int daemon_cmd_post(en_oc_mqtt_daemon_cmd cmd, void *arg)
static int config_parameter_release(oc_mqtt_tiny_cb_t *cb)
{

osal_free(cb->config.id);
if(cb->config.id != NULL)
osal_free(cb->config.id);

osal_free(cb->config.pwd);
if(cb->config.pwd != NULL)
osal_free(cb->config.pwd);

osal_free(cb->config.server_addr);
if(cb->config.server_addr != NULL)
osal_free(cb->config.server_addr);

osal_free(cb->config.server_port);
if(cb->config.server_port != NULL)
osal_free(cb->config.server_port);

osal_free(cb->bs_cb.hubserver_addr);
if(cb->bs_cb.hubserver_addr != NULL)
osal_free(cb->bs_cb.hubserver_addr);

osal_free(cb->bs_cb.hubserver_port);
if(cb->bs_cb.hubserver_port != NULL)
osal_free(cb->bs_cb.hubserver_port);

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

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


if((NULL == cb->config.id) || (NULL == cb->config.pwd) ||\
(NULL == cb->config.server_addr) || (NULL == cb->config.server_port))
{
Expand Down Expand Up @@ -897,6 +902,11 @@ static int tiny_config(oc_mqtt_config_t *config)
int ret = en_oc_mqtt_err_parafmt;
if(NULL != config)
{
if((NULL == config->id) || (NULL == config->pwd) ||\
(NULL == config->server_addr) || (NULL == config->server_port))
{
return ret;
}
ret = daemon_cmd_post(en_oc_mqtt_daemon_cmd_connect,config);
}
return ret;
Expand Down
50 changes: 37 additions & 13 deletions test/c_dependcy/test_case_oc_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,33 +592,57 @@ static int ts_oc_mqtt_config(char *message, int len)
{
osal_free(pparas->id);
}
pparas->id = osal_malloc(strlen(pchTmp)+1);
memcpy(pparas->id, pchTmp, strlen(pchTmp));
pparas->id[strlen(pchTmp)] = '\0';
printf("120--id is %s\n",pparas->id);
if((!memcmp(pchTmp, "NULL", strlen(pchTmp))) ||
(!memcmp(pchTmp, "null", strlen(pchTmp))))
{
pparas->id = NULL;
}
else
{
pparas->id = osal_malloc(strlen(pchTmp)+1);
memcpy(pparas->id, pchTmp, strlen(pchTmp));
pparas->id[strlen(pchTmp)] = '\0';
printf("120--id is %s\n",pparas->id);
}

pchTmp = strtok_r(NULL, "|", &pchStrTmpIn);
if(pparas->passwd != NULL)
{
osal_free(pparas->passwd);
}
pparas->passwd = osal_malloc(strlen(pchTmp)+1);
memcpy(pparas->passwd, pchTmp, strlen(pchTmp));
pparas->passwd[strlen(pchTmp)] = '\0';
if((!memcmp(pchTmp, "NULL", strlen(pchTmp))) ||
(!memcmp(pchTmp, "null", strlen(pchTmp))))
{
pparas->passwd = NULL;
}
else
{
pparas->passwd = osal_malloc(strlen(pchTmp)+1);
memcpy(pparas->passwd, pchTmp, strlen(pchTmp));
pparas->passwd[strlen(pchTmp)] = '\0';
printf("140--passwd is %s\n",pparas->passwd);
}

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


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

if(pparas->cbname != NULL)
{
osal_free(pparas->cbname);
}
pparas->cbname = osal_malloc(strlen(pchTmp)+1);
memcpy(pparas->cbname, pchTmp, strlen(pchTmp));
pparas->cbname[strlen(pchTmp)] = '\0';
printf("125--cbname is %s\n",pparas->cbname);

if((!memcmp(pchTmp, "NULL", strlen(pchTmp))) ||
(!memcmp(pchTmp, "null", strlen(pchTmp))))
{
pparas->cbname = NULL;
}
else
{
pparas->cbname = osal_malloc(strlen(pchTmp)+1);
memcpy(pparas->cbname, pchTmp, strlen(pchTmp));
pparas->cbname[strlen(pchTmp)] = '\0';
printf("125--cbname is %s\n",pparas->cbname);
}


config.boot_mode = pparas->boot_mode;
Expand Down
165 changes: 31 additions & 134 deletions test/python/test_linux_oc_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ def test_linux_oc_mqtt_register():
assert(result.test_id == mqtt_testid.TEST_OC_MQTT_REGISTER)
assert(result.ret_code == 0)


# typedef enum
# {
# en_oc_mqtt_err_ok = 0, ///< this means the status ok
# en_oc_mqtt_err_parafmt, ///< this means the parameter err format
# en_oc_mqtt_err_network, ///< this means the network wrong status
# en_oc_mqtt_err_conversion, ///< this means the mqtt version err
# en_oc_mqtt_err_conclientid, ///< this means the client id is err
# en_oc_mqtt_err_conserver, ///< this means the server refused the service for some reason(likely the id and pwd)
# en_oc_mqtt_err_conuserpwd, ///< bad user name or passwd
# en_oc_mqtt_err_conclient, ///< the client id /user/pwd is right, but does not allowed
# en_oc_mqtt_err_subscribe, ///< this means subscribe the topic failed
# en_oc_mqtt_err_publish, ///< this means subscribe the topic failed
# en_oc_mqtt_err_configured, ///< this means we has configured, please deconfigured it and then do configure again
# en_oc_mqtt_err_noconfigured, ///< this means we have not configure it yet,so could not connect
# en_oc_mqtt_err_noconected, ///< this means the connection has not been built, so you could not send data
# en_oc_mqtt_err_gethubaddrtimeout, ///< this means get the hub address timeout
# en_oc_mqtt_err_sysmem, ///< this means the system memory is not enough
# en_oc_mqtt_err_system, ///< this means that the system porting may have some problem,maybe not install yet
# en_oc_mqtt_err_last,
# }en_oc_mqtt_err_code_t;

def test_linux_oc_mqtt_config_invalid():
fname = sys._getframe().f_code.co_name
#invalid server ip
Expand All @@ -68,7 +90,7 @@ def test_linux_oc_mqtt_config_invalid():
CB_NAME)
assert(result)
assert(result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
assert(result.ret_code == -1)
assert(result.ret_code == 1) #en_oc_mqtt_err_parafmt

# invalid server port
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_CONFIG, fname,
Expand All @@ -78,7 +100,7 @@ def test_linux_oc_mqtt_config_invalid():
CB_NAME)
assert (result)
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
assert (result.ret_code == -1)
assert (result.ret_code == 1) #en_oc_mqtt_err_parafmt

# invalid security type
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_CONFIG, fname,
Expand All @@ -88,7 +110,7 @@ def test_linux_oc_mqtt_config_invalid():
CB_NAME)
assert (result)
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
assert (result.ret_code == -1)
assert (result.ret_code == 1) #en_oc_mqtt_err_parafmt


# invalid ep name
Expand All @@ -99,7 +121,7 @@ def test_linux_oc_mqtt_config_invalid():
CB_NAME)
assert (result)
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
assert (result.ret_code == -1)
assert (result.ret_code == 1) #en_oc_mqtt_err_parafmt

# invalid passwd
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_CONFIG, fname,
Expand All @@ -109,7 +131,7 @@ def test_linux_oc_mqtt_config_invalid():
CB_NAME)
assert (result)
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
assert (result.ret_code == -1)
assert (result.ret_code == 1) #en_oc_mqtt_err_parafmt



Expand Down Expand Up @@ -260,126 +282,6 @@ def test_linux_oc_mqtt_deconfig_static():
# assert (status == "OFFLINE")
print(status)


def test_linux_oc_mqtt_config_bs():
fname = sys._getframe().f_code.co_name
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_CONFIG, fname,
en_oc_boot_strap_mode_client_initialize, LIFE_TIME,
BS_SERVER_IP4, BS_SERVER_PORT, SEC_TYPE,
mqtt_device_info.BS_MQTT_EP_USER, mqtt_device_info.BS_MQTT_EP_PASSWD,
CB_NAME)
assert(result)
assert(result.test_id == mqtt_testid.TEST_OC_MQTT_CONFIG)
assert(result.ret_code == 0)
time.sleep(2)
# oc_mqtt_device_status_jduge_b(mqtt_device_info.BS_MQTT_DEVICEID, "ONLINE")

def test_linux_oc_mqtt_json_fmt_req_bs():
fname = sys._getframe().f_code.co_name
#must send 4 items here
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_JSON_FMT_REQ, fname,
"LED", 0, #LED is serviceId,0 means en_oc_mqtt_has_more_no
"LED1", 0, 1,#LED1 is item name, 0 means en_key_value_type_int, 1 means value
"LED2", 0, 2,
"LED3", 0, 35,
"LED4", 0, 78
)
assert (result)
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_JSON_FMT_REQ)
assert (result.ret_code == 1)
print("----------------------------------------------")
print("hhhh")
print(json.loads(result.data).get("data")[0])
print("----------------------------------------------")
# typedef enum
# {
# en_oc_mqtt_has_more_no = 0,
# en_oc_mqtt_has_more_yes = 1,
# }en_oc_mqtt_has_more;
# typedef enum
# {
# en_key_value_type_int = 0,
# en_key_value_type_string,
# en_key_value_type_array,
# }en_value_type;
# parase received json data
# {
# "msgType": "deviceReq",
# "hasMore": 0,
# "data": [{
# "serviceId": "LED",
# "serviceData": {
# "LED1": 1,
# "LED2": 2,
# "LED2": 35,
# "LED2": 78
# }
# }]
# }

assert (json.loads(result.data).get("msgType") == "deviceReq")
assert (json.loads(result.data).get("hasMore") == 0)
data = json.loads(result.data).get("data")[0]
assert (data["serviceId"] == "LED")
print("+++++++++++++++++++++++++++++++++++++++++++")
print(result.data)
print(data)
print(data["serviceData"])
print("+++++++++++++++++++++++++++++++++++++++++++")
assert (data.get("serviceData").get("LED1") == 1)
assert (data.get("serviceData").get("LED2") == 2)
assert (data.get("serviceData").get("LED3") == 35)
assert (data.get("serviceData").get("LED4") == 78)


def test_linux_oc_mqtt_json_fmt_res_bs():
fname = sys._getframe().f_code.co_name
# must send 1 items here
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_JSON_FMT_RES, fname,
0, 0, 1, # 0 means en_oc_mqtt_has_more_no, second means no error, 1 means mid
"body_para", 1, "body_para" # body_para is item name, second means string type, third means value
)
print(result)
assert (result)
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_JSON_FMT_RES)
assert (result.ret_code == 1)
print(result.data)

# {
# "msgType": "deviceRsp",
# "mid": 1,
# "errcode": 0,
# "hasMore": 0,
# "body": {
# "body_para": "body_para"
# }
# }

assert (json.loads(result.data).get("msgType") == "deviceRsp")
assert (json.loads(result.data).get("mid") == 1)
assert (json.loads(result.data).get("errcode") == 0)
assert (json.loads(result.data).get("hasMore") == 0)
assert (json.loads(result.data).get("body").get("body_para") == "body_para")


def test_linux_oc_mqtt_publish_bs():
fname = sys._getframe().f_code.co_name
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_PUBLISH, fname)
assert (result)
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_PUBLISH)
assert (result.ret_code == 0)

def test_linux_oc_mqtt_deconfig_bs():
fname = sys._getframe().f_code.co_name
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_DECONFIG, fname)
assert (result)
assert (result.test_id == mqtt_testid.TEST_OC_MQTT_DECONFIG)
assert (result.ret_code == 0)
time.sleep(15)
# oc_mqtt_device_status_jduge_b(mqtt_device_info.BS_MQTT_DEVICEID, "OFFLINE")



def test_linux_oc_mqtt_deinit():
fname = sys._getframe().f_code.co_name
result = ts_call_single(mqtt_testid.TEST_OC_MQTT_DEINIT, fname)
Expand All @@ -394,16 +296,11 @@ def test_linux_oc_mqtt_deinit():
print("hello world")
test_linux_oc_mqtt_init()
test_linux_oc_mqtt_register()
# test_linux_oc_mqtt_config_invalid()
test_linux_oc_mqtt_config_invalid()
test_linux_oc_mqtt_config_static()
# test_linux_oc_mqtt_json_fmt_req_static()
# test_linux_oc_mqtt_json_fmt_res_static()
# test_linux_oc_mqtt_report_static()
test_linux_oc_mqtt_json_fmt_req_static()
test_linux_oc_mqtt_json_fmt_res_static()
test_linux_oc_mqtt_publish_static()
test_linux_oc_mqtt_sendcmd_static()
test_linux_oc_mqtt_deconfig_static()
# test_linux_oc_mqtt_config_bs()
# test_linux_oc_mqtt_json_fmt_req_bs()
# test_linux_oc_mqtt_json_fmt_res_bs()
# test_linux_oc_mqtt_report_bs()
# test_linux_oc_mqtt_deconfig_bs()
test_linux_oc_mqtt_deinit()
Loading

0 comments on commit 76a0001

Please sign in to comment.