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
22
23 #define TEST_IPV4 1
24 static u32_t g_coapTestTaskid = -1;
25 static int g_servRunning = 0;
26 static coap_context_t *g_servCtx = NULL;
27 /*
28 * The resource 'hello' GET method handler
29 */
HelloHandler(coap_context_t * ctx,struct coap_resource_t * resource,coap_session_t * session,coap_pdu_t * request,coap_binary_t * token,coap_string_t * query,coap_pdu_t * response)30 static void HelloHandler(coap_context_t *ctx, struct coap_resource_t *resource, coap_session_t *session,
31 coap_pdu_t *request, coap_binary_t *token, coap_string_t *query, coap_pdu_t *response)
32 {
33 unsigned char buf[3];
34 /* response with text "Hello World!" */
35 const char* responseData = "Hello World! CoAP";
36 size_t len = 0;
37 unsigned char *data = NULL;
38 (void)ctx;
39 (void)resource;
40 (void)session;
41 response->code = COAP_RESPONSE_CODE(205); /* 返回值205,代表连接成功 */
42 coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
43 coap_encode_var_safe(buf, 3, COAP_MEDIATYPE_TEXT_PLAIN), buf); /* 3个字节长度 */
44 coap_add_data(response, strlen(responseData), (unsigned char *)responseData);
45 if (coap_get_data(request, &len, &data)) {
46 printf("Hello World! CoAP\n");
47 }
48 }
CoapServerThread(UINT32 uwParam1,UINT32 uwParam2,UINT32 uwParam3,UINT32 uwParam4)49 void CoapServerThread(UINT32 uwParam1, UINT32 uwParam2, UINT32 uwParam3, UINT32 uwParam4)
50 {
51 coap_context_t* ctx;
52 (void)uwParam2;
53 (void)uwParam3;
54 (void)uwParam4;
55 printf("[%s][%d] thread running\n", __FUNCTION__, __LINE__);
56 ctx = (coap_context_t*)uwParam1;
57 while (g_servRunning == 1) {
58 hi_sleep(1000); /* 休眠1000ms */
59 coap_check_notify_lwip(ctx);
60 }
61 if (g_servCtx != NULL) {
62 coap_free_context_lwip(g_servCtx);
63 g_servCtx = NULL;
64 }
65 printf("[%s][%d] thread exit\n", __FUNCTION__, __LINE__);
66 return;
67 }
CoapServerStart(void)68 int CoapServerStart(void)
69 {
70 TSK_INIT_PARAM_S stappTask;
71 UINT32 ret;
72 coap_address_t serv_addr;
73 coap_resource_t* hello_resource;
74 if (g_servRunning == 1) {
75 return 0;
76 }
77 g_servRunning = 1;
78 /* Prepare the CoAP server socket */
79 coap_address_init(&serv_addr);
80 #if TEST_IPV4
81 #else
82 ip_addr_set_any(true, &(serv_addr.addr));
83 #endif
84 serv_addr.port = COAP_DEFAULT_PORT;
85 g_servCtx = coap_new_context_lwip(&serv_addr);
86 if (!g_servCtx) {
87 return -1;
88 }
89 /* Initialize the hello resource */
90 hello_resource = coap_resource_init(coap_make_str_const("hello"), 0);
91 coap_register_handler(hello_resource, COAP_REQUEST_GET, HelloHandler);
92 coap_add_resource(g_servCtx, hello_resource);
93 /* create a thread task */
94 stappTask.pfnTaskEntry = (TSK_ENTRY_FUNC)CoapServerThread;
95 stappTask.uwStackSize = 10 * LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; /* task为10 */
96 stappTask.pcName = "coap_test_task";
97 stappTask.usTaskPrio = 11; /* task为11 */
98 stappTask.uwResved = LOS_TASK_STATUS_DETACHED;
99 stappTask.auwArgs[0] = (UINT32)g_servCtx;
100 printf("task create CoapServerThread\n");
101 ret = LOS_TaskCreate(&g_coapTestTaskid, &stappTask);
102 if (ret != 0) {
103 dprintf("CoapServerThread create failed ! \n");
104 return -1;
105 }
106 return 0;
107 }
CoapServerStop(void)108 void CoapServerStop(void)
109 {
110 if (g_servRunning == 0) {
111 printf("[%s][%d] not running\n", __FUNCTION__, __LINE__);
112 return;
113 }
114 if (g_servCtx != NULL) {
115 coap_free_context_lwip(g_servCtx);
116 g_servCtx = NULL;
117 }
118 g_servRunning = 0;
119 printf("[%s][%d] stopped\n", __FUNCTION__, __LINE__);
120 return;
121 }
NetDemoTest(void)122 void NetDemoTest(void)
123 {
124 printf("coapserviceTest start\r\n");
125 CoapServerStart();
126 }