|
说漏了leds的操作,这个也比较典型,基本上是rest构架的核心体现,get、post思维,。。
leds资源,服务,代码如下
- /* Global Variables ----------------------------------------------------------*/
- RESOURCE(res_leds,
- "title="LEDs: ?color=r|g|b, POST/PUT mode=on|off";rt="Control"",
- NULL,
- res_post_put_handler,
- res_post_put_handler,
- NULL);
- /* Local Functions -----------------------------------------------------------*/
- /**
- * brief res_post_put_handler
- * note POST/PUT处理
- * param None
- * retval None
- */
- static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
- {
- size_t len = 0;
- const char *color = NULL;
- const char *mode = NULL;
- uint8_t led = 0;
- int success = 1;
-
- if ((len = REST.get_query_variable(request, "color", &color))) {
- PRINTF("color %.*s\n", len, color);
-
- if (strncmp(color, "r", len) == 0) {
- led = LEDS_RED;
- } else if (strncmp(color, "g", len) == 0) {
- led = LEDS_GREEN;
- } else if (strncmp(color, "b", len) == 0) {
- led = LEDS_BLUE;
- } else {
- success = 0;
- }
- } else {
- success = 0;
- }
-
- if (success && (len = REST.get_post_variable(request, "mode", &mode))) {
- PRINTF("mode %s\n", mode);
-
- if (strncmp(mode, "on", len) == 0) {
- leds_on(led);
- } else if (strncmp(mode, "off", len) == 0) {
- leds_off(led);
- } else {
- success = 0;
- }
- } else {
- success = 0;
- }
-
- if (!success) {
- REST.set_response_status(response, REST.status.BAD_REQUEST);
- }
- }
复制代码
为了演示,我这里使用的是同一个灯也就是LED0咯,加个继电器控制实际的灯就免了,太low了,不想折腾。
这里比较全,可以post直接搞定也可以用数据区,随你怎么办,同时CoAP提供java库,所以会玩的不要担心什么web,apps啊。
|
|