1 /*
2 * Copyright (c) 2018 Intel Corporation
3 * Copyright (c) 2023 Basalte bv
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/logging/log.h>
9 LOG_MODULE_DECLARE(net_coap_service_sample);
10
11 #include <zephyr/sys/printk.h>
12 #include <zephyr/net/coap_service.h>
13 #include <zephyr/net/coap_link_format.h>
14
core_get(struct coap_resource * resource,struct coap_packet * request,struct sockaddr * addr,socklen_t addr_len)15 static int core_get(struct coap_resource *resource,
16 struct coap_packet *request,
17 struct sockaddr *addr, socklen_t addr_len)
18 {
19 static const char dummy_str[] = "Just a test\n";
20 uint8_t data[CONFIG_COAP_SERVER_MESSAGE_SIZE];
21 struct coap_packet response;
22 uint8_t token[COAP_TOKEN_MAX_LEN];
23 uint16_t id;
24 uint8_t tkl;
25 int r;
26
27 id = coap_header_get_id(request);
28 tkl = coap_header_get_token(request, token);
29
30 r = coap_packet_init(&response, data, sizeof(data),
31 COAP_VERSION_1, COAP_TYPE_ACK, tkl, token,
32 COAP_RESPONSE_CODE_CONTENT, id);
33 if (r < 0) {
34 return r;
35 }
36
37 r = coap_packet_append_payload_marker(&response);
38 if (r < 0) {
39 return r;
40 }
41
42 r = coap_packet_append_payload(&response, (uint8_t *)dummy_str,
43 sizeof(dummy_str));
44 if (r < 0) {
45 return r;
46 }
47
48 r = coap_resource_send(resource, &response, addr, addr_len, NULL);
49
50 return r;
51 }
52
53 static const char * const core_1_path[] = { "core1", NULL };
54 static const char * const core_1_attributes[] = {
55 "title=\"Core 1\"",
56 "rt=core1",
57 NULL,
58 };
59 COAP_RESOURCE_DEFINE(core_1, coap_server,
60 {
61 .get = core_get,
62 .path = core_1_path,
63 .user_data = &((struct coap_core_metadata) {
64 .attributes = core_1_attributes,
65 }),
66 });
67
68 static const char * const core_2_path[] = { "core2", NULL };
69 static const char * const core_2_attributes[] = {
70 "title=\"Core 2\"",
71 "rt=core2",
72 NULL,
73 };
74 COAP_RESOURCE_DEFINE(core_2, coap_server,
75 {
76 .get = core_get,
77 .path = core_2_path,
78 .user_data = &((struct coap_core_metadata) {
79 .attributes = core_2_attributes,
80 }),
81 });
82