sensord: clean up sf_common.h/sensor_common.h/sensor_logs.h
[platform/core/system/sensord.git] / src / shared / cpacket.cpp
1 /*
2  * libsensord-share
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <new>
24 #include <sensor_logs.h>
25 #include <cpacket.h>
26 #include <command_common.h>
27
28 cpacket::cpacket()
29 {
30         m_packet = NULL;
31 }
32
33 cpacket::cpacket(size_t size)
34 {
35         m_packet = NULL;
36         set_payload_size(size);
37 }
38
39 cpacket::~cpacket()
40 {
41         delete[] (char*)m_packet;
42 }
43
44 void cpacket::set_cmd(int cmd)
45 {
46         if (!m_packet)
47                 set_payload_size(0);
48
49         m_packet->cmd = cmd;
50 }
51
52 int cpacket::cmd(void)
53 {
54         if (!m_packet)
55                 return CMD_NONE;
56
57         return m_packet->cmd;
58 }
59
60 void *cpacket::data(void)
61 {
62         if (!m_packet)
63                 return NULL;
64
65         return m_packet->data;
66 }
67
68 void *cpacket::packet(void)
69 {
70         return (void*)m_packet;
71 }
72
73 size_t cpacket::size(void)
74 {
75         if (!m_packet)
76                 return 0;
77
78         return m_packet->size + sizeof(packet_header);
79 }
80
81 size_t cpacket::payload_size(void)
82 {
83         if (!m_packet)
84                 return 0;
85
86         return m_packet->size;
87 }
88
89 void cpacket::set_payload_size(size_t size)
90 {
91         int prev_cmd = CMD_NONE;
92
93         if (m_packet) {
94                 prev_cmd = m_packet->cmd;
95                 delete []m_packet;
96         }
97
98         m_packet = (packet_header*) new(std::nothrow) char[size + sizeof(packet_header)];
99         retm_if (!m_packet, "Failed to allocate memory");
100         m_packet->size = size;
101
102         if (prev_cmd != CMD_NONE)
103                 m_packet->cmd = prev_cmd;
104 }