| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsip-simple/presence.h> |
| 20 |
#include <pjsip-simple/errno.h> |
| 21 |
#include <pjsip/sip_msg.h> |
| 22 |
#include <pjsip/sip_transport.h> |
| 23 |
#include <pj/guid.h> |
| 24 |
#include <pj/log.h> |
| 25 |
#include <pj/os.h> |
| 26 |
#include <pj/pool.h> |
| 27 |
#include <pj/string.h> |
| 28 |
|
| 29 |
|
| 30 |
#define THIS_FILE "presence_body.c" |
| 31 |
|
| 32 |
|
| 33 |
static const pj_str_t STR_APPLICATION = { "application", 11 }; |
| 34 |
static const pj_str_t STR_PIDF_XML = { "pidf+xml", 8 }; |
| 35 |
static const pj_str_t STR_XPIDF_XML = { "xpidf+xml", 9 }; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static int pres_print_body(struct pjsip_msg_body *msg_body, |
| 44 |
char *buf, pj_size_t size) |
| 45 |
{ |
| 46 |
return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size, |
| 47 |
PJ_TRUE); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len) |
| 55 |
{ |
| 56 |
PJ_UNUSED_ARG(len); |
| 57 |
return pj_xml_clone( pool, (const pj_xml_node*) data); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool, |
| 66 |
const pjsip_pres_status *status, |
| 67 |
const pj_str_t *entity, |
| 68 |
pjsip_msg_body **p_body ) |
| 69 |
{ |
| 70 |
pjpidf_pres *pidf; |
| 71 |
pjsip_msg_body *body; |
| 72 |
unsigned i; |
| 73 |
|
| 74 |
|
| 75 |
pidf = pjpidf_create(pool, entity); |
| 76 |
|
| 77 |
|
| 78 |
for (i=0; i<status->info_cnt; ++i) { |
| 79 |
|
| 80 |
pjpidf_tuple *pidf_tuple; |
| 81 |
pjpidf_status *pidf_status; |
| 82 |
pj_str_t id; |
| 83 |
|
| 84 |
|
| 85 |
if (status->info[i].id.slen == 0) { |
| 86 |
|
| 87 |
|
| 88 |
id.ptr = (char*)pj_pool_alloc(pool, pj_GUID_STRING_LENGTH()+2); |
| 89 |
id.ptr += 2; |
| 90 |
pj_generate_unique_string(&id); |
| 91 |
id.ptr -= 2; |
| 92 |
id.ptr[0] = 'p'; |
| 93 |
id.ptr[1] = 'j'; |
| 94 |
id.slen += 2; |
| 95 |
} else { |
| 96 |
id = status->info[i].id; |
| 97 |
} |
| 98 |
|
| 99 |
pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id); |
| 100 |
|
| 101 |
|
| 102 |
if (status->info[i].contact.slen) |
| 103 |
pjpidf_tuple_set_contact(pool, pidf_tuple, |
| 104 |
&status->info[i].contact); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
pidf_status = pjpidf_tuple_get_status(pidf_tuple); |
| 109 |
pjpidf_status_set_basic_open(pidf_status, |
| 110 |
status->info[i].basic_open); |
| 111 |
|
| 112 |
|
| 113 |
#if defined(PJSIP_PRES_PIDF_ADD_TIMESTAMP) && PJSIP_PRES_PIDF_ADD_TIMESTAMP |
| 114 |
if (PJSIP_PRES_PIDF_ADD_TIMESTAMP) { |
| 115 |
char buf[50]; |
| 116 |
int tslen = 0; |
| 117 |
pj_time_val tv; |
| 118 |
pj_parsed_time pt; |
| 119 |
pj_status_t s; |
| 120 |
|
| 121 |
pj_gettimeofday(&tv); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
s = pj_time_local_to_gmt(&tv); |
| 127 |
if (s != PJ_SUCCESS) { |
| 128 |
PJ_PERROR(4,(THIS_FILE,s, |
| 129 |
"Warning: failed to convert PIDF time to GMT")); |
| 130 |
} |
| 131 |
pj_time_decode( &tv, &pt); |
| 132 |
|
| 133 |
tslen = pj_ansi_snprintf(buf, sizeof(buf), |
| 134 |
"%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
| 135 |
pt.year, pt.mon+1, pt.day, |
| 136 |
pt.hour, pt.min, pt.sec, pt.msec); |
| 137 |
if (tslen > 0 && tslen < (int)sizeof(buf)) { |
| 138 |
pj_str_t time = pj_str(buf); |
| 139 |
pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time); |
| 140 |
} |
| 141 |
} |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
if (status->info_cnt) { |
| 147 |
pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid); |
| 148 |
} |
| 149 |
|
| 150 |
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); |
| 151 |
body->data = pidf; |
| 152 |
body->content_type.type = STR_APPLICATION; |
| 153 |
body->content_type.subtype = STR_PIDF_XML; |
| 154 |
body->print_body = &pres_print_body; |
| 155 |
body->clone_data = &xml_clone_data; |
| 156 |
|
| 157 |
*p_body = body; |
| 158 |
|
| 159 |
return PJ_SUCCESS; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
PJ_DEF(pj_status_t) pjsip_pres_create_xpidf( pj_pool_t *pool, |
| 168 |
const pjsip_pres_status *status, |
| 169 |
const pj_str_t *entity, |
| 170 |
pjsip_msg_body **p_body ) |
| 171 |
{ |
| 172 |
|
| 173 |
|
| 174 |
pjxpidf_pres *xpidf; |
| 175 |
pjsip_msg_body *body; |
| 176 |
|
| 177 |
PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported " |
| 178 |
"by PJSIP")); |
| 179 |
|
| 180 |
|
| 181 |
xpidf = pjxpidf_create(pool, entity); |
| 182 |
|
| 183 |
|
| 184 |
if (status->info_cnt > 0) |
| 185 |
pjxpidf_set_status( xpidf, status->info[0].basic_open); |
| 186 |
else |
| 187 |
pjxpidf_set_status( xpidf, PJ_FALSE); |
| 188 |
|
| 189 |
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); |
| 190 |
body->data = xpidf; |
| 191 |
body->content_type.type = STR_APPLICATION; |
| 192 |
body->content_type.subtype = STR_XPIDF_XML; |
| 193 |
body->print_body = &pres_print_body; |
| 194 |
body->clone_data = &xml_clone_data; |
| 195 |
|
| 196 |
*p_body = body; |
| 197 |
|
| 198 |
return PJ_SUCCESS; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata, |
| 207 |
pj_pool_t *pool, |
| 208 |
pjsip_pres_status *pres_status) |
| 209 |
{ |
| 210 |
return pjsip_pres_parse_pidf2((char*)rdata->msg_info.msg->body->data, |
| 211 |
rdata->msg_info.msg->body->len, |
| 212 |
pool, pres_status); |
| 213 |
} |
| 214 |
|
| 215 |
PJ_DEF(pj_status_t) pjsip_pres_parse_pidf2(char *body, unsigned body_len, |
| 216 |
pj_pool_t *pool, |
| 217 |
pjsip_pres_status *pres_status) |
| 218 |
{ |
| 219 |
pjpidf_pres *pidf; |
| 220 |
pjpidf_tuple *pidf_tuple; |
| 221 |
|
| 222 |
pidf = pjpidf_parse(pool, body, body_len); |
| 223 |
if (pidf == NULL) |
| 224 |
return PJSIP_SIMPLE_EBADPIDF; |
| 225 |
|
| 226 |
pres_status->info_cnt = 0; |
| 227 |
|
| 228 |
pidf_tuple = pjpidf_pres_get_first_tuple(pidf); |
| 229 |
while (pidf_tuple && pres_status->info_cnt < PJSIP_PRES_STATUS_MAX_INFO) { |
| 230 |
pjpidf_status *pidf_status; |
| 231 |
|
| 232 |
pres_status->info[pres_status->info_cnt].tuple_node = |
| 233 |
pj_xml_clone(pool, pidf_tuple); |
| 234 |
|
| 235 |
pj_strdup(pool, |
| 236 |
&pres_status->info[pres_status->info_cnt].id, |
| 237 |
pjpidf_tuple_get_id(pidf_tuple)); |
| 238 |
|
| 239 |
pj_strdup(pool, |
| 240 |
&pres_status->info[pres_status->info_cnt].contact, |
| 241 |
pjpidf_tuple_get_contact(pidf_tuple)); |
| 242 |
|
| 243 |
pidf_status = pjpidf_tuple_get_status(pidf_tuple); |
| 244 |
if (pidf_status) { |
| 245 |
pres_status->info[pres_status->info_cnt].basic_open = |
| 246 |
pjpidf_status_is_basic_open(pidf_status); |
| 247 |
} else { |
| 248 |
pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE; |
| 249 |
} |
| 250 |
|
| 251 |
pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple ); |
| 252 |
pres_status->info_cnt++; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
pjrpid_get_element(pidf, pool, &pres_status->info[0].rpid); |
| 257 |
|
| 258 |
return PJ_SUCCESS; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata, |
| 266 |
pj_pool_t *pool, |
| 267 |
pjsip_pres_status *pres_status) |
| 268 |
{ |
| 269 |
return pjsip_pres_parse_xpidf2((char*)rdata->msg_info.msg->body->data, |
| 270 |
rdata->msg_info.msg->body->len, |
| 271 |
pool, pres_status); |
| 272 |
} |
| 273 |
|
| 274 |
PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf2(char *body, unsigned body_len, |
| 275 |
pj_pool_t *pool, |
| 276 |
pjsip_pres_status *pres_status) |
| 277 |
{ |
| 278 |
pjxpidf_pres *xpidf; |
| 279 |
|
| 280 |
xpidf = pjxpidf_parse(pool, body, body_len); |
| 281 |
if (xpidf == NULL) |
| 282 |
return PJSIP_SIMPLE_EBADXPIDF; |
| 283 |
|
| 284 |
pres_status->info_cnt = 1; |
| 285 |
|
| 286 |
pj_strdup(pool, |
| 287 |
&pres_status->info[0].contact, |
| 288 |
pjxpidf_get_uri(xpidf)); |
| 289 |
pres_status->info[0].basic_open = pjxpidf_get_status(xpidf); |
| 290 |
pres_status->info[0].id.slen = 0; |
| 291 |
pres_status->info[0].tuple_node = NULL; |
| 292 |
|
| 293 |
return PJ_SUCCESS; |
| 294 |
} |
| 295 |
|
| 296 |
|