• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "coap.h"
17 #include "net.h"
18 #include "los_task.h"
19 #include "lwip/tcpip.h"
20 #include "lwip/udp.h"
21 #include "coap_session_internal.h"
22 #include "iot_config.h"
23 
24 #define TEST_IPV4 1
25 #define DHCP_COAP_TOKEN_LEN (4)
26 static coap_context_t* g_cliCtx = NULL;
27 /* The response handler */
MessageHandler(struct coap_context_t * ctx,coap_session_t * session,coap_pdu_t * sent,coap_pdu_t * received,coap_tid_t id)28 static void MessageHandler(struct coap_context_t *ctx, coap_session_t *session, coap_pdu_t *sent,
29     coap_pdu_t *received, coap_tid_t id)
30 {
31     unsigned char* data;
32     size_t dataLen;
33     (void)ctx;
34     (void)session;
35     (void)sent;
36     (void)received;
37     (void)id;
38     if (COAP_RESPONSE_CLASS(received->code) == 2) { /* 获取的数据2 */
39         if (coap_get_data(received, &dataLen, &data)) {
40             printf("Received data\n");
41         }
42     }
43 }
44 
CoapClientStart(void)45 int CoapClientStart(void)
46 {
47     coap_address_t src_addr;
48     if (g_cliCtx != NULL) {
49         return 0;
50     }
51     /* Prepare coap socket */
52     coap_address_init(&src_addr);
53 #if TEST_IPV4
54     ip_addr_set_any(false, &(src_addr.addr));
55 #else
56     ip_addr_set_any(true, &(src_addr.addr));
57 #endif
58     src_addr.port = 23456; /* 主机端口号23456 */
59     g_cliCtx = coap_new_context_lwip(&src_addr);
60     if (!g_cliCtx) {
61         return -1;
62     }
63     /* Set the response handler */
64     coap_register_response_handler(g_cliCtx, MessageHandler);
65     return 0;
66 }
CoapClientStop(void)67 void CoapClientStop(void)
68 {
69     if (g_cliCtx != NULL) {
70         coap_free_context_lwip(g_cliCtx);
71         g_cliCtx = NULL;
72     }
73     printf("[%s][%d] stopped\n", __FUNCTION__, __LINE__);
74     return;
75 }
76 /* to create a new token value depending on time */
CoapNewToken(u16_t msg_id,u8_t * token,u8_t token_len)77 s32_t CoapNewToken(u16_t msg_id, u8_t *token, u8_t token_len)
78 {
79     u32_t now_ms;
80     if ((token == NULL) || (token_len < DHCP_COAP_TOKEN_LEN)) {
81         return -1;
82     }
83     now_ms = sys_now();
84     token[0] = (u8_t)(msg_id); /* 0数据 */
85     token[1] = (u8_t)(msg_id >> 8); /* 1数据,8数据 */
86     token[2] = (u8_t)(now_ms); /* 2数据 */
87     token[3] = (u8_t)(now_ms >> 8); /* 3数据,8数据 */
88     return 0;
89 }
90 #define STRING_LEN (7)
CoapClientSendMsg(char * dst)91 int CoapClientSendMsg(char* dst)
92 {
93     coap_address_t dst_addr, listen_addr;
94     static coap_uri_t uri;
95     coap_pdu_t* request;
96     coap_session_t *session = NULL;
97     char serverUri[128] = {0};
98     u8_t temp_token[DHCP_COAP_TOKEN_LEN] = {0};
99     unsigned char getMethod = COAP_REQUEST_GET;
100     /* The destination endpoint */
101     coap_address_init(&dst_addr);
102     printf("[%s][%d] server : %s\n", __FUNCTION__, __LINE__, dst);
103 
104     if (!ipaddr_aton(dst, &(dst_addr.addr))) {
105         return -1;
106     }
107     dst_addr.port = COAP_DEFAULT_PORT;
108     /* try to reuse existed session */
109     session = coap_session_get_by_peer(g_cliCtx, &dst_addr, 0);
110     if (session == NULL) {
111         coap_address_init(&listen_addr);
112         ip_addr_set_any(false, &(listen_addr.addr));
113         listen_addr.port = 23456; /* 监听端口号23456 */
114         session = coap_new_client_session(g_cliCtx, &listen_addr, &dst_addr, COAP_PROTO_UDP);
115         if (session == NULL) {
116             printf("[%s][%d] new client session failed\n", __FUNCTION__, __LINE__);
117             return -1;
118         }
119         session->sock.pcb = g_cliCtx->endpoint;
120         SESSIONS_ADD(g_cliCtx->endpoint->sessions, session);
121     }
122  /* Prepare the request */
123     strcpy_s(serverUri, STRING_LEN, "/hello");
124     coap_split_uri((unsigned char *)serverUri, strlen(serverUri), &uri);
125     request = coap_new_pdu(session);
126     if (request == NULL) {
127         printf("[%s][%d] get pdu failed\n", __FUNCTION__, __LINE__);
128         return -1;
129     }
130     request->type = COAP_MESSAGE_CON;
131     request->tid = coap_new_message_id(session);
132     (void)CoapNewToken(request->tid, temp_token, DHCP_COAP_TOKEN_LEN);
133     if (coap_add_token(request, DHCP_COAP_TOKEN_LEN, temp_token) == 0) {
134         printf("[%s][%d] add token failed\n", __FUNCTION__, __LINE__);
135     }
136     request->code = getMethod;
137     coap_add_option(request, COAP_OPTION_URI_PATH, uri.path.length, uri.path.s);
138     char requestData[64] = {0};
139     (void)snprintf_s(requestData, sizeof(requestData), sizeof(requestData)-1, "%s", "Hello coap");
140     coap_add_data(request, 4 + strlen((const char *)(requestData + 4)), (unsigned char *)requestData);
141     coap_send_lwip(session, request);
142     return 0;
143 }
144 
CoapClientSend(void)145 int CoapClientSend(void)
146 {
147     if (g_cliCtx == NULL) {
148         return -1;
149     }
150     /* argv[0] is server_addr */
151     CoapClientSendMsg(PARAM_SERVER_ADDR); /* 主机IP地址 */
152     return 0;
153 }
154 
NetDemoTest(void)155 void NetDemoTest(void)
156 {
157     printf("coapclientTest start\r\n");
158     CoapClientStart();
159     CoapClientSend();
160 }