This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
wasm-micro-runtime/core/app-mgr/app-manager/message.c
2019-05-17 17:15:25 +08:00

99 lines
2.4 KiB
C

/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "app_manager.h"
#include "app_manager_host.h"
#include "event.h"
#include "attr_container.h"
#include "bh_memory.h"
#include "coap_ext.h"
#if 0
bool send_coap_packet_to_host(coap_packet_t * packet)
{
int size;
uint8_t *buf;
size = coap_serialize_message_tcp(&packet, &buf);
if (!buf || size == 0)
return false;
app_manager_host_send_msg(buf, size);
bh_free(buf);
return true;
}
#endif
bool send_request_to_host(request_t *msg)
{
if (COAP_EVENT == msg->action && !event_is_registered(msg->url)) {
app_manager_printf("Event is not registered\n");
return false;
}
int size;
char * packet = pack_request(msg, &size);
if (packet == NULL)
return false;
app_manager_host_send_msg(REQUEST_PACKET, packet, size);
free_req_resp_packet(packet);
return true;
}
bool send_response_to_host(response_t *response)
{
int size;
char * packet = pack_response(response, &size);
if (packet == NULL)
return false;
app_manager_host_send_msg(RESPONSE_PACKET, packet, size);
free_req_resp_packet(packet);
return true;
}
bool send_error_response_to_host(int mid, int status, const char *msg)
{
int payload_len = 0;
attr_container_t *payload = NULL;
response_t response[1] = { 0 };
if (msg) {
payload = attr_container_create("");
if (payload) {
attr_container_set_string(&payload, "error message", msg);
payload_len = attr_container_get_serialize_length(payload);
}
}
set_response(response, status,
FMT_ATTR_CONTAINER, (const char *)payload, payload_len);
response->mid = mid;
send_response_to_host(response);
if (payload)
attr_container_destroy(payload);
return true;
}