1 /* usrgsm.c v 0.1
2  *
3  * Mark Edwards <medwards@greenside.demon.co.uk>
4  *
5  * usrgsm is a utility that will convert to and from the encapsulated
6  * GSM format used by USRobotics Sportster Vi modems (with personal voice
7  * mail.
8  * This has not been tested on Sportster Voice modems, but I don't suspect
9  * any difference. No support for adpcm.
10  *
11  * usrgsm will convert to and from the format used in the 'gsm-1.0' utilities
12  * that have the 'toast','untoast' and 'tcat' binaries. This allows for
13  * converting from the raw USRobotics format out to .au or anything else
14  * that you have a conversion utility for. I have found that using untoast -o
15  * on the converted file to produce an .au appears to work well.
16  *
17  * usage of this util is:
18  *
19  * usrgsm < in.rmd > out.gsm
20  *
21  * gsmusr < in.gsm > out.rmd
22  *
23  * Note. Files are playable on the USRobotics after conversion to .rmd format.
24  *
25  * $Id: usrgsm.c,v 1.3 1999/01/23 15:17:08 marcs Exp $
26  *
27  */
28 
29 #include <stdio.h>
30 #include <fcntl.h>
31 
32 void *buf;
33 short numelem=0;
34 
35 int  f_usrtogsm     =0;
36 
37 typedef unsigned char    gsm_byte;
38 typedef gsm_byte    gsm_frame[33];
39 typedef gsm_byte    usr_frame[38];
40 
41 gsm_byte USR_Header[]={  0x52,0x4D,0x44,0x31,0x55,0x53,0x20,0x52,
42                0x6F,0x62,0x6F,0x74,0x69,0x63,0x73,0x00,
43                0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,
44                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
45 
46 
47 int
GSM_to_USR(usr_frame pUFr,gsm_frame pGFr)48 GSM_to_USR(usr_frame pUFr,gsm_frame pGFr)
49 {
50   void* ptr=(void*)pUFr;
51   pUFr[0]=pUFr[1]=0xFE;
52   ptr++; ptr++;
53   memcpy((void*)ptr,(void*)pGFr,(size_t)33);
54   pUFr[35]=0x00;
55   pUFr[36]=pUFr[37]=0xAE;
56   return 0;
57 }
58 
59 /* This function strips the encapsulated USRobotics frame
60    and returns the 33 raw GSM Bytes */
61 int
USR_to_GSM(usr_frame pUFr,gsm_frame pGFr)62 USR_to_GSM(usr_frame pUFr,gsm_frame pGFr)
63 {
64   void* ptr=(void*)pUFr;
65   ptr++; ptr++;
66   if (!((pUFr[0]==0xB6 && pUFr[1]==0xB6) || (pUFr[0]==0xFE && pUFr[1]==0xFE)))
67     return -1;
68   if (!(pUFr[35]==0x00 && pUFr[36]==0xA5 && pUFr[37]==0xA5))
69     return -1;
70   memcpy((void*)pGFr,(void*)ptr,(size_t)33);
71   (*(unsigned char*)pGFr)|=(0xD0);
72   return 0;
73 }
74 
75 /* Stip the Header information from the vgetty USRobotics file. */
76 int
StripUSRHeader(FILE * inf)77 StripUSRHeader(FILE *inf)
78 {
79   numelem=fread(buf,1,32,inf);
80   if (numelem<32)
81     return -1;
82   return 0;
83 }
84 
85 int
AddUSRHeader(FILE * of)86 AddUSRHeader(FILE *of)
87 {
88   fwrite((void*)USR_Header,1,sizeof(USR_Header),of);
89   return 0;
90 }
91 
92 void
main(int argc,char ** argv)93 main (int argc, char** argv)
94 {
95   gsm_frame g;
96   usr_frame r;
97   int nelem=0;
98   int fcount=0;
99 
100   /* Lets see what we were called as...*/
101   if (!strcmp(argv[0],"usrgsm"))
102    {
103     fprintf(stderr,"Converting USR format to GSM format.\n");
104     f_usrtogsm=1;
105    }
106   else if (!strcmp(argv[0],"gsmusr"))
107    {
108     fprintf(stderr,"Converting GSM format to USR format.\n");
109     f_usrtogsm=0;
110    }
111   else
112    {
113     fprintf(stderr,"Use as gsmusr or usrgsm only.\n");
114     exit (0);
115    }
116 
117   /* Scratchpad */
118   buf = (void*)malloc((size_t)1024);
119 
120   if (f_usrtogsm)
121    {
122     StripUSRHeader(stdin);
123     while (!feof(stdin))
124      {
125       nelem=fread(r,1,sizeof(r),stdin);
126       if (nelem<(int)sizeof(r))
127        {
128         if (nelem==0)
129           goto done;
130         fprintf(stderr,"Unable to read full USR frame.\n");
131         fprintf(stderr,"Only read %i bytes.\n",nelem);
132         goto done;
133        }
134       if (USR_to_GSM(r,g))
135        {
136         fprintf(stderr,"Unable to convert USR Frame.\n");
137         goto done;
138        }
139       nelem=fwrite(g,1,sizeof(g),stdout);
140       if (nelem<(int)sizeof(g))
141        {
142         fprintf(stderr,"Unable to write full gsm frame.\n");
143         goto done;
144        }
145       else
146        fcount++;
147      }
148    }
149   else
150    {
151     AddUSRHeader(stdout);
152     while (!feof(stdin))
153      {
154       nelem=fread(g,1,sizeof(g),stdin);
155       if (nelem<(int)sizeof(g))
156        {
157         if (nelem==0)
158           goto done;
159         fprintf(stderr,"Unable to read full gsm frame.\n");
160         fprintf(stderr,"Only read %i bytes.\n",nelem);
161         goto done;
162        }
163       if (GSM_to_USR(r,g))
164        {
165         fprintf(stderr,"Unable to convert gsm Frame.\n");
166         goto done;
167        }
168       nelem=fwrite(r,1,sizeof(r),stdout);
169       if (nelem<(int)sizeof(r))
170        {
171         fprintf(stderr,"Unable to write full USR frame.\n");
172         goto done;
173        }
174       else
175        fcount++;
176      }
177    }
178 done:
179    free(buf);
180    fprintf(stderr,"Frames Converted=%i\n",fcount);
181    fflush(stdout);
182    fflush(stderr);
183    exit(0);
184 }
185 
186