1import ctypes 2import sys 3import os 4import array 5 6global arrayelementsize 7 8 9def Generate_CRC(inlist): 10 crc = int(~0)&0xffffffff 11 for i in range(len(inlist)-1): 12 crc = crc ^ (int(inlist[i]&0xffffffff)) 13 for j in range(4*8): 14 crc = ((int(crc >> 1)&0xffffffff) ^ int(0xEDB88320 & ((-(crc & 1))&0xffffffff) ))&0xffffffff 15 16 crc=(~crc)&0xffffffff 17 18 19 if (crc>0x7fffffff): 20 crc=-(0x7fffffff-(crc&0x7fffffff)+1) 21 22 print("Calculated CRC= ", crc) 23 print("Actual CRC=", inlist[len(inlist)-1]) 24 25 return(crc) 26 27 28def convertto24bit(binarydata): 29 30 if (binarydata[14]==2): 31 print("16 bit detected") 32 binarydata[14]=4 33 elif (binarydata[14]==4): 34 print("24 bit detected") 35 return(1) 36 else: 37 print("unknown config") 38 return(0) 39 40 ilist = [16,18,20] 41 42 for i in ilist: 43 if (binarydata[i]==2): 44 binarydata[i]=4 45 elif (binarydata[14]==4): 46 print("unknown config 4") 47 return(0) 48 else: 49 print("unknown config") 50 return(0) 51 52 for i in range(21,len(binarydata)-1): 53 if (binarydata[i]==17): 54 if (binarydata[i+1]==0): 55 binarydata[i]=53 56 if (binarydata[i+1]==1): 57 binarydata[i]=53 58 if (binarydata[i+1]==2): 59 binarydata[i]=53 60 if (binarydata[i+1]==3): 61 binarydata[i]=53 62 if (binarydata[i]==16): 63 if (binarydata[i+1]==5): 64 binarydata[i]=52 65 if (binarydata[i+1]==4): 66 binarydata[i]=52 67 68 crc=Generate_CRC(binarydata) 69 binarydata[len(binarydata)-1]=crc 70 return(1) 71 72 73def detectbitpersample(binarydata): 74 75 if (binarydata[14]==4): 76 print("24 bit detected") 77 elif (binarydata[14]==2): 78 print("16 bit detected") 79 else: 80 print("unknown config") 81 82def convertto16bit(binarydata): 83 84 if (binarydata[14]==4): 85 print("24 bit detected") 86 binarydata[14]=2 87 elif (binarydata[14]==2): 88 print("16 bit detected") 89 return(1) 90 else: 91 print("unknown config") 92 return(0) 93 94 ilist = [16,18,20] 95 96 for i in ilist: 97 if (binarydata[i]==4): 98 binarydata[i]=2 99 elif (binarydata[14]==2): 100 print("unknown config 4") 101 return(0) 102 else: 103 print("unknown config") 104 return(0) 105 106 for i in range(21,len(binarydata)-1): 107 if (binarydata[i]==53): 108 if (binarydata[i+1]==0): 109 binarydata[i]=17 110 if (binarydata[i+1]==1): 111 binarydata[i]=17 112 if (binarydata[i+1]==2): 113 binarydata[i]=17 114 if (binarydata[i+1]==3): 115 binarydata[i]=17 116 if (binarydata[i]==52): 117 if (binarydata[i+1]==5): 118 binarydata[i]=16 119 if (binarydata[i+1]==4): 120 binarydata[i]=16 121 122 crc=Generate_CRC(binarydata) 123 binarydata[len(binarydata)-1]=crc 124 return(1) 125 126 127def binary2header(fileinput,fileinput1,fileoutput,to24bit,fileoutput1,fileoutput2): 128 binarydata=array.array(arrayelementsize) 129 file=open(fileinput,'rb') 130 if file==-1: 131 print('error cannot open file for reading:',fileinput) 132 return 133 file.seek(0,2) 134 arraysize=int(file.tell()/4) 135 print('length of config string RX:', arraysize) 136 file.seek(0,0) 137 binarydata.fromfile(file,arraysize) 138 file.close() 139 140 binarydata1=array.array(arrayelementsize) 141 file1=open(fileinput1,'rb') 142 if file1==-1: 143 print('error cannot open file for reading:',fileinput) 144 return 145 file1.seek(0,2) 146 arraysize1=int(file1.tell()/4) 147 print('length of config string TX:', arraysize1) 148 file1.seek(0,0) 149 binarydata1.fromfile(file1,arraysize1) 150 file1.close() 151 152 153 154 if (to24bit==0): 155 detectbitpersample(binarydata) 156 157 elif (to24bit==1): 158 result=convertto16bit(binarydata) 159 if (result==0): 160 print("Problem converting to 16bit") 161 exit(0) 162 163 elif (to24bit==2): 164 result=convertto24bit(binarydata) 165 if (result==0): 166 print("Problem converting to 24bit") 167 exit(0) 168 169 170 fileo=open(fileoutput,'wt') 171 if (fileo==-1): 172 print('error cannot open file for writing:',fileoutput) 173 return 174 175 fileo.write('\n\n\n#define CSPL_CONFIG_RX ') 176 177 for i in range(len(binarydata)-1): 178 fileo.write(str(binarydata[i])+', ') 179 180 fileo.write(str(binarydata[len(binarydata)-1])+'\n\n\n\n') 181 182 fileo.write('\n\n\n#define CSPL_CONFIG_TX ') 183 184 for i in range(len(binarydata1)-1): 185 fileo.write(str(binarydata1[i])+', ') 186 187 fileo.write(str(binarydata1[len(binarydata1)-1])+'\n\n\n\n') 188 189 fileo.close() 190 print('wrote file:',fileoutput) 191 192 if (fileoutput1!=None): 193 fileo=open(fileoutput1,'wb') 194 if (fileo==-1): 195 print('error cannot open file for writing:',fileoutput1) 196 return 197 198 binarydata.tofile(fileo) 199 fileo.close() 200 print('wrote file:',fileoutput1) 201 202 if (fileoutput2!=None): 203 fileo=open(fileoutput2,'wb') 204 if (fileo==-1): 205 print('error cannot open file for writing:',fileoutput2) 206 return 207 208 binarydata1.tofile(fileo) 209 fileo.close() 210 print('wrote file:',fileoutput2) 211 212 213 214def header2binary(fileinput,fileoutput,to24bit,fileoutput1,fileoutput2): 215 RX=array.array(arrayelementsize) 216 TX=array.array(arrayelementsize) 217 file=open(fileinput,'rt') 218 if file==-1: 219 print('error cannot open file for reading:',fileinput) 220 return 221 222 while True: 223 line=file.readline() 224 sline=line.split() 225 if (len(sline)>1): 226 if (sline[1]=='CSPL_CONFIG_RX'): 227 for r in range(2,len(sline)-1,1): 228 RX.append(int(sline[r][0:len(sline[r])-1])) 229 RX.append(int(sline[len(sline)-1])) 230 231 if (sline[1]=='CSPL_CONFIG_TX'): 232 for r in range(2,len(sline)-1,1): 233 TX.append(int(sline[r][0:len(sline[r])-1])) 234 TX.append(int(sline[len(sline)-1])) 235 236 if (line == ''): 237 break 238 239 file.close() 240 241 if (to24bit==0): 242 detectbitpersample(RX) 243 244 elif (to24bit==1): 245 result=convertto16bit(RX) 246 if (result==0): 247 print("Problem converting to 16bit") 248 exit(0) 249 250 elif (to24bit==2): 251 result=convertto24bit(RX) 252 if (result==0): 253 print("Problem converting to 24bit") 254 exit(0) 255 256 if (fileoutput!=None): 257 fileo=open(fileoutput,'wt') 258 if (fileo==-1): 259 print('error cannot open file for writing:',fileoutput) 260 return 261 if (len(RX)>0): 262 fileo.write('\n\n\n#define CSPL_CONFIG_RX ') 263 264 for i in range(len(RX)-1): 265 fileo.write(str(RX[i])+', ') 266 267 fileo.write(str(RX[len(RX)-1])+'\n\n\n\n') 268 269 if (len(TX)>0): 270 fileo.write('\n\n\n#define CSPL_CONFIG_TX ') 271 272 for i in range(len(TX)-1): 273 fileo.write(str(TX[i])+', ') 274 275 fileo.write(str(TX[len(TX)-1])+'\n\n\n\n') 276 277 fileo.close() 278 print('wrote file:',fileoutput) 279 280 if (len(RX)>0): 281 if (fileoutput1!=None): 282 fileo=open(fileoutput1,'wb') 283 if (fileo==-1): 284 print('error cannot open file for writing:',fileoutput1) 285 return 286 RX.tofile(fileo) 287 fileo.close() 288 print('wrote file:',fileoutput1) 289 290 if (len(TX)>0): 291 if (fileoutput2!=None): 292 fileo=open(fileoutput2,'wb') 293 if (fileo==-1): 294 print('error cannot open file for writing:',fileoutput2) 295 return 296 TX.tofile(fileo) 297 fileo.close() 298 print('wrote file:',fileoutput2) 299 300 pass 301 302 303def usage(): 304 print('Possible usages: ') 305 print('1. Convert from binary to text header file:') 306 print('\t\targs: bin <binary file RX> <binary file TX> <output text file>') 307 308 print('2. Convert from binary from 16bit to 24bit then output to text header file:') 309 print('\t\targs: bin16 <binary file RX> <binary file TX> <output text file>') 310 311 print('3. Convert from binary from 16bit to 24bit then output to text header file and binary file') 312 print('\t\targs: bin16 <binary file RX> <binary file TX> <output text file> <binary rx file> <binary tx file>') 313 314 print('4. Convert from binary from 24bit to 16bit then output to text header file:') 315 print('\t\targs: bin24 <binary file RX> <binary file TX> <output text file>') 316 317 print('5. Convert from binary from 24bit to 16bit then output to text header file and binary file') 318 print('\t\targs: bin24 <binary file RX> <binary file TX> <output text file> <binary rx file> <binary tx file>') 319 320 print('6. Convert from text to binary file') 321 print('\t\targs: txt <text file> <output binary file RX> <output binary file TX>') 322 323 print('7. Convert from text 16bit to 24bit to text file') 324 print('\t\targs: txt16 <text file> <text output file>') 325 326 print('8. Convert from text 16bit to 24bit to text file and binary files') 327 print('\t\targs: txt16 <text file> <text output file> <output binary file RX> <output binary file TX>') 328 329 print('9. Convert from text 24bit to 16bit to text file') 330 print('\t\targs: txt24 <text file> <text output file>') 331 332 print('10. Convert from text 24bit to 16bit to text file and binary files') 333 print('\t\targs: txt24 <text file> <text output file> <output binary file RX> <output binary file TX>') 334 335 336 337 return 338 339 340 341def main(argv): 342 343 print('CSPL config string conversion used for android system') 344 345 if (len(argv)<4): 346 usage() 347 return 348 349 global arrayelementsize 350 351 print("python version: ",sys.version_info) 352 print("Current Os name: ",os.name) 353 354 validsize=0 355 for r in ['l','i']: 356 arrayelementsize=r 357 t=array.array(arrayelementsize) 358 if (t.itemsize==4): 359 validsize=1 360 break 361 362 if (validsize!=1): 363 print("Expected element size donot match firmware requirement") 364 365 366 if (argv[1]=='bin'): 367 binary2header(argv[2],argv[3],argv[4],0) 368 elif (argv[1]=='txt'): 369 header2binary(argv[2],None,0,argv[3],argv[4]) 370 elif (argv[1]=='bin16'): 371 if (len(argv)==5): 372 binary2header(argv[2],argv[3],argv[4],1,None,None) 373 elif (len(argv)==7): 374 binary2header(argv[2],argv[3],argv[4],1,argv[5],argv[6]) 375 elif (argv[1]=='bin24'): 376 if (len(argv)==5): 377 binary2header(argv[2],argv[3],argv[4],2,None,None) 378 elif (len(argv)==7): 379 binary2header(argv[2],argv[3],argv[4],2,argv[5],argv[6]) 380 elif (argv[1]=='txt16'): 381 if (len(argv)==4): 382 header2binary(argv[2],argv[3],1,None,None) 383 elif (len(argv)==6): 384 header2binary(argv[2],argv[3],1,argv[4],argv[5]) 385 elif (argv[1]=='txt24'): 386 if (len(argv)==4): 387 header2binary(argv[2],argv[3],2,None,None) 388 elif (len(argv)==6): 389 header2binary(argv[2],argv[3],2,argv[4],argv[5]) 390 391 else : 392 usage() 393 return 394 395 396if __name__ == "__main__": 397 main(sys.argv) 398