Back to index

im-sdk  12.3.91
im-protocol-version.c
Go to the documentation of this file.
00001 /* im-protocol-version.c */
00002 
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <sys/types.h>
00007 
00008 #include <iiimp-data.h>
00009 #include <iiimp-opcode.h>
00010 
00011 #include "iiimp-dataP.h"
00012 
00013 /*
00014   IM_PROTOCOL_VERSION / V3
00015 
00016   im-id:      CARD16
00017   version:    CARD8
00018   UNUSED:     Pad(3) = 1
00019 */
00020 
00021 IIIMP_message *
00022 iiimp_protocol_version_new(
00023     IIIMP_data_s *   data_s,
00024     IIIMP_card16     im_id,
00025     int                     protocol_version
00026 )
00027 {
00028     IIIMP_message *  m;
00029 
00030     REQUIRE_PROTOCOL_VERSION(data_s, 3);
00031 
00032     m = (IIIMP_message *)malloc(sizeof (IIIMP_message));
00033     if (NULL == m) {
00034        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00035        return NULL;
00036     }
00037     m->opcode = IM_PROTOCOL_VERSION;
00038     m->im_id = im_id;
00039     m->ic_id = -1;
00040 
00041     if (data_s->protocol_version < protocol_version) {
00042        data_s->status = IIIMP_DATA_INVALID;
00043        return NULL;
00044     }
00045 
00046     m->v.protocol_version.number = protocol_version;
00047 
00048     return m;
00049 }
00050 
00051 
00052 void
00053 iiimp_protocol_version_delete(
00054     IIIMP_data_s * data_s,
00055     IIIMP_message * m
00056 )
00057 {
00058     if (NULL == m) return;
00059     free(m);
00060     return;
00061 }
00062 
00063 
00064 uchar_t *
00065 iiimp_protocol_version_pack(
00066     IIIMP_data_s *   data_s,
00067     IIIMP_card16     im_id,
00068     int                     protocol_version,
00069     size_t *         buf_size
00070 )
00071 {
00072     size_t           nbyte;
00073     int                     length;
00074     uchar_t *        buf;
00075     size_t           rest;
00076     uchar_t *        p;
00077 
00078     nbyte = 4; /* im-id(2) + version(1) + UNUSED(1) */
00079 
00080     length = (nbyte >> 2);
00081     *buf_size = (1 + 3 + nbyte);
00082 
00083     buf = (uchar_t *) malloc(1 + 3 + nbyte);
00084     if (NULL == buf) {
00085        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00086        return NULL;
00087     }
00088 
00089     PUT_PACKET_HEADER(buf, IM_PROTOCOL_VERSION, length);
00090 
00091     rest = nbyte;
00092     p = (buf + 4);
00093 
00094     PUTU16(im_id, rest, p, data_s->byte_swap);
00095     PUTU8(protocol_version, rest, p, data_s->byte_swap);
00096     PUTU8(0, rest, p, data_s->byte_swap);
00097 
00098     return buf;
00099 }
00100 
00101 
00102 IIIMP_message *
00103 iiimp_protocol_version_unpack(
00104     IIIMP_data_s *   data_s,
00105     IIIMP_card7             opcode,
00106     size_t *         nbyte,
00107     const uchar_t ** ptr)
00108 {
00109     IIIMP_message *         m;
00110     IIIMP_protocol_version_v *     v;
00111     size_t                  rest;
00112     const uchar_t *         p;
00113 
00114     rest = *nbyte;
00115     p = *ptr;
00116 
00117     if (rest < (2 + 1 + 1)) {
00118        data_s->status = IIIMP_DATA_INVALID;
00119        return NULL;
00120     }
00121 
00122     m = (IIIMP_message *)malloc(sizeof (IIIMP_message));
00123     if (NULL == m) {
00124        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00125        return NULL;
00126     }
00127     v = &(m->v.protocol_version);
00128 
00129     m->opcode = opcode;
00130     GETU16(m->im_id, rest, p, data_s->byte_swap);
00131     m->ic_id = -1;
00132     GETU8(v->number, rest, p, data_s->byte_swap);
00133     SKIP8(rest, p);
00134 
00135     *nbyte = rest;
00136     *ptr = p;
00137 
00138     return m;
00139 }
00140 
00141 
00142 void
00143 iiimp_protocol_version_print(
00144     IIIMP_data_s *   data_s,
00145     IIIMP_message *  m)
00146 {
00147     IIIMP_protocol_version_v *     v;
00148 
00149     v = &(m->v.protocol_version);
00150 
00151     iiimp_message_header_print(data_s, m->opcode, m->im_id, -1);
00152     (void)fprintf(data_s->print_fp, "\tprotocol_version = 0x%02x\n",
00153                 v->number);
00154 }
00155 
00156 /* Local Variables: */
00157 /* c-file-style: "iiim-project" */
00158 /* End: */