~phablet-team/libdsme/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
   @file messages.h

   Message type definitions for DSME.
   <p>
   Copyright (C) 2004-2009 Nokia Corporation.

   @author Ari Saastamoinen
   @author Ismo Laitinen <ismo.laitinen@nokia.com>
   @author Semi Malinen <semi.malinen@nokia.com>

   This file is part of Dsme.

   Dsme is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License
   version 2.1 as published by the Free Software Foundation.

   Dsme is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with Dsme.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef DSME_MESSAGES_H
#define DSME_MESSAGES_H

#include <stddef.h>
#include <sys/types.h>

#ifdef __cplusplus
extern "C" {
#endif


#define DSME_MSG_NEW(T) \
  (T*)dsmemsg_new(DSME_MSG_ID_(T), sizeof(T), 0)

#define DSME_MSG_NEW_WITH_EXTRA(T, E) \
  (T*)dsmemsg_new(DSME_MSG_ID_(T), sizeof(T), (E))

#ifdef __cplusplus
#define DSME_MSG_INIT(T)               \
  (T){                                 \
    /* line_size_ = */ sizeof(T),      \
    /* size_      = */ sizeof(T),      \
    /* type_      = */ DSME_MSG_ID_(T) \
  }
#else
#define DSME_MSG_INIT(T)         \
  (T){                           \
    .line_size_ = sizeof(T),      \
    .size_      = sizeof(T),      \
    .type_      = DSME_MSG_ID_(T) \
  }
#endif


#define DSME_MSG_ID_(T)      T ## _ID_
#define DSME_MSG_ENUM(T, ID) DSME_MSG_ID_(T) = ID


/* TODO: either rename DSMEMSG->DSME_MSG or DSME_MSG->DSMEMSG */
#define DSMEMSG_EXTRA(M)                                              \
  (((const dsmemsg_generic_t*)(M))->line_size_ -                      \
   ((const dsmemsg_generic_t*)(M))->size_ > 0 ?                       \
     (void*)(((char*)(M)) + ((const dsmemsg_generic_t*)(M))->size_) : \
     (void*)0)

#define DSMEMSG_EXTRA_SIZE(M)                       \
  (((const dsmemsg_generic_t*)(M))->line_size_ -    \
    ((const dsmemsg_generic_t*)(M))->size_ > 0 ?    \
      ((const dsmemsg_generic_t*)(M))->line_size_ - \
        ((const dsmemsg_generic_t*)(M))->size_ :    \
      0)

#define DSMEMSG_CAST(T, M)                                       \
 ((((const dsmemsg_generic_t*)(M))->size_ == sizeof(T)) ?        \
    (((const dsmemsg_generic_t*)(M))->type_ == DSME_MSG_ID_(T) ? \
      (T*)(M) : 0) : 0)


/**
 * @defgroup message_if Message passing interface for modules
 * In addition to these functions, dsmesock_send() can be used from modules.
 */

#define DSMEMSG_PRIVATE_FIELDS \
  u_int32_t line_size_;        \
  u_int32_t size_;             \
  u_int32_t type_;

/**
   Generic message type
   @ingroup message_if
*/
typedef struct dsmemsg_generic_t {
  DSMEMSG_PRIVATE_FIELDS
} dsmemsg_generic_t;


/**
   Specific message type that is sent when socket is closed
   @ingroup message_if
*/
typedef struct {
  DSMEMSG_PRIVATE_FIELDS
  u_int8_t reason;
} DSM_MSGTYPE_CLOSE;

/**
   Close reasons
   @ingroup dsmesock_client
*/
enum {
  TSMSG_CLOSE_REASON_OOS = 0, /* Protocol out of sync (local) */
  TSMSG_CLOSE_REASON_EOF = 1, /* EOF read from socket (local) */
  TSMSG_CLOSE_REASON_REQ = 2, /* Peer requests close */
  TSMSG_CLOSE_REASON_ERR = 3  /* Undefined error conditions or read after close */
};


typedef dsmemsg_generic_t DSM_MSGTYPE_GET_VERSION;
typedef dsmemsg_generic_t DSM_MSGTYPE_DSME_VERSION;

/* TA stands for Type Approval: */
typedef dsmemsg_generic_t DSM_MSGTYPE_SET_TA_TEST_MODE;


enum {
    /* DSME Protocol messages 000000xx */
    DSME_MSG_ENUM(DSM_MSGTYPE_CLOSE,            0x00000001),

    /* misc */
    DSME_MSG_ENUM(DSM_MSGTYPE_GET_VERSION,      0x00001100),
    DSME_MSG_ENUM(DSM_MSGTYPE_DSME_VERSION,     0x00001101),
    DSME_MSG_ENUM(DSM_MSGTYPE_SET_TA_TEST_MODE, 0x00001102),
};

void* dsmemsg_new(u_int32_t id, size_t size, size_t extra);

u_int32_t dsmemsg_id(const dsmemsg_generic_t* msg);

#ifdef __cplusplus
}
#endif

#endif