~cyphermox/ubuntu/natty/ofono/release-0.41

« back to all changes in this revision

Viewing changes to gisi/message.c

  • Committer: Mathieu Trudel-Lapierre
  • Date: 2011-02-11 02:17:20 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: mathieu-tl@ubuntu.com-20110211021720-cvxc3erw1keomunj
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  oFono - Open Source Telephony
 
4
 *
 
5
 *  Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License version 2 as
 
9
 *  published by the Free Software Foundation.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 *
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <stdint.h>
 
27
#include <string.h>
 
28
#include <errno.h>
 
29
#include <arpa/inet.h>
 
30
#include <glib.h>
 
31
 
 
32
#include "message.h"
 
33
 
 
34
int g_isi_msg_version_major(const GIsiMessage *msg)
 
35
{
 
36
        if (msg == NULL || msg->version == NULL)
 
37
                return -1;
 
38
 
 
39
        return msg->version->major;
 
40
}
 
41
 
 
42
int g_isi_msg_version_minor(const GIsiMessage *msg)
 
43
{
 
44
        if (msg == NULL || msg->version == NULL)
 
45
                return -1;
 
46
 
 
47
        return msg->version->minor;
 
48
}
 
49
 
 
50
int g_isi_msg_error(const GIsiMessage *msg)
 
51
{
 
52
        return msg != NULL ? -msg->error : -EINVAL;
 
53
}
 
54
 
 
55
const char *g_isi_msg_strerror(const GIsiMessage *msg)
 
56
{
 
57
        return strerror(-g_isi_msg_error(msg));
 
58
}
 
59
 
 
60
uint8_t g_isi_msg_resource(const GIsiMessage *msg)
 
61
{
 
62
        if (msg == NULL || msg->addr == NULL)
 
63
                return 0;
 
64
 
 
65
        return msg->addr->spn_resource;
 
66
}
 
67
 
 
68
uint16_t g_isi_msg_object(const GIsiMessage *msg)
 
69
{
 
70
        if (msg == NULL || msg->addr == NULL)
 
71
                return 0;
 
72
 
 
73
        return (msg->addr->spn_dev << 8) | msg->addr->spn_obj;
 
74
}
 
75
 
 
76
uint8_t g_isi_msg_id(const GIsiMessage *msg)
 
77
{
 
78
        const uint8_t *buf;
 
79
 
 
80
        if (msg == NULL || msg->data == NULL || msg->len < 2)
 
81
                return 0;
 
82
 
 
83
        buf = msg->data;
 
84
 
 
85
        return buf[1];
 
86
}
 
87
 
 
88
uint8_t g_isi_msg_utid(const GIsiMessage *msg)
 
89
{
 
90
        const uint8_t *buf;
 
91
 
 
92
        if (msg == NULL || msg->data == NULL || msg->len < 2)
 
93
                return 0;
 
94
 
 
95
        buf = msg->data;
 
96
 
 
97
        return buf[0];
 
98
}
 
99
 
 
100
size_t g_isi_msg_data_len(const GIsiMessage *msg)
 
101
{
 
102
        if (msg == NULL || msg->data == NULL)
 
103
                return 0;
 
104
 
 
105
        return msg->len - 2;
 
106
}
 
107
 
 
108
const void *g_isi_msg_data(const GIsiMessage *msg)
 
109
{
 
110
        if (msg == NULL || msg->data == NULL)
 
111
                return NULL;
 
112
 
 
113
        return (void *)msg->data + 2;
 
114
}
 
115
 
 
116
gboolean g_isi_msg_data_get_byte(const GIsiMessage *msg, unsigned offset,
 
117
                                        uint8_t *byte)
 
118
{
 
119
        const uint8_t *buf = g_isi_msg_data(msg);
 
120
 
 
121
        if (buf == NULL || g_isi_msg_data_len(msg) < offset)
 
122
                return FALSE;
 
123
 
 
124
        if (byte != NULL)
 
125
                *byte = buf[offset];
 
126
 
 
127
        return TRUE;
 
128
}
 
129
 
 
130
gboolean g_isi_msg_data_get_word(const GIsiMessage *msg, unsigned offset,
 
131
                                        uint16_t *word)
 
132
{
 
133
        const uint8_t *buf = g_isi_msg_data(msg);
 
134
        uint16_t val;
 
135
 
 
136
        if (buf == NULL || g_isi_msg_data_len(msg) < offset + 1)
 
137
                return FALSE;
 
138
 
 
139
        memcpy(&val, buf + offset, sizeof(uint16_t));
 
140
 
 
141
        if (word != NULL)
 
142
                *word = ntohs(val);
 
143
 
 
144
        return TRUE;
 
145
}
 
146
 
 
147
gboolean g_isi_msg_data_get_struct(const GIsiMessage *msg, unsigned offset,
 
148
                                        const void **type, size_t len)
 
149
{
 
150
        if (g_isi_msg_data_len(msg) < offset + len)
 
151
                return FALSE;
 
152
 
 
153
        if (type != NULL)
 
154
                *type = g_isi_msg_data(msg) + offset;
 
155
 
 
156
        return TRUE;
 
157
}