4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <sensor_log.h>
27 #define UNDEFINED_TYPE -2
29 static std::atomic<uint64_t> sequence(0);
31 message::message(size_t capacity)
33 , m_capacity(capacity)
34 , m_msg((char *)malloc(sizeof(char) * capacity))
37 m_header.id = sequence++;
38 m_header.type = UNDEFINED_TYPE;
39 m_header.length = m_size;
43 message::message(const void *msg, size_t sz)
49 m_header.id = sequence++;
50 m_header.type = UNDEFINED_TYPE;
51 m_header.length = m_size;
55 message::message(const message &msg)
57 , m_capacity(msg.m_capacity)
58 , m_msg((char *)malloc(sizeof(char) * msg.m_capacity))
61 ::memcpy(&m_header, &msg.m_header, sizeof(message_header));
62 ::memcpy(m_msg, msg.m_msg, msg.m_size);
65 message::message(int error)
71 m_header.id = sequence++;
72 m_header.type = UNDEFINED_TYPE;
79 if (m_msg && ref_cnt == 0) {
85 void message::enclose(const void *msg, const size_t sz)
93 ::memcpy(reinterpret_cast<char *>(m_msg), msg, sz);
98 void message::enclose(int error)
100 m_header.err = error;
105 void message::disclose(void *msg)
110 ::memcpy(msg, m_msg, m_size);
113 uint32_t message::type(void)
115 return m_header.type;
118 void message::set_type(uint32_t msg_type)
120 m_header.type = msg_type;
123 size_t message::size(void)
128 /* TODO: remove ref/unref and use reference counting automatically */
129 void message::ref(void)
134 void message::unref(void)
138 if (ref_cnt > 0 || !m_msg)
146 int message::ref_count(void)
151 message_header *message::header(void)
156 char *message::body(void)