/** 
 * Malloc off of the Server Heap memory for sending a message
 * 
 * @param msg            Pointer to Malloced message
 * @param message_length Length of memory to Malloc
 * 
 * @return SIRF_SUCCESS or SIRF_GPRS_AT_COMMAND_SERVER_MEMORY_ERROR
 */
tSIRF_RESULT SIRF_GPRS_AT_COMMAND_SERVER_MsgMalloc(
   gprs_msg_t               **     msg,
   tSIRF_UINT32                    message_length )
{
   fifo_link_t     * link;

   if (NULL == gprs_at_command.listener_mx)
   {
      return SIRF_GPRS_AT_COMMAND_SERVER_NOT_CREATED;
   }

   /* Post the data on the message queue to be handled by the GPRS thread */
   link = (fifo_link_t*)Heap_Malloc(gprs_at_command.msg_heap,
                               sizeof(fifo_link_t) + 
                               sizeof(gprs_msg_t) + 
                               message_length);
   
   if (NULL == link)
   {
      return SIRF_GPRS_AT_COMMAND_SERVER_MEMORY_ERROR;
   }

   *msg                      = (gprs_msg_t*)((tSIRF_UINT32)link + sizeof(fifo_link_t));
   link->data                = (void*)*msg;
   (*msg)->message_structure = (void*)((tSIRF_UINT32)*msg + sizeof(gprs_msg_t));
   (*msg)->message_length = message_length;
   return SIRF_SUCCESS;
}
예제 #2
0
파일: main.cpp 프로젝트: leiqunni/v2iewx
// ---------------------------------------------------------------------------
//
HBITMAP __fastcall TForm1::SPI_LoadImage(String fileName) {
	/* 対応プラグインの検索 */
	for (int i = 0; i < hSPI->Count; i++) { // プラグイン関数の取得
		SPI_ISSUPPORTED spi_issupported = (SPI_ISSUPPORTED) GetProcAddress((HMODULE) hSPI->Items[i], SPIPROC_ISSUPPORTED);
		SPI_GETPICTURE spi_getpicture = (SPI_GETPICTURE) GetProcAddress((HMODULE) hSPI->Items[i], SPIPROC_GETPICTURE);

		if (spi_issupported == NULL || spi_getpicture == NULL) {
			continue;
		}

		// File内容をロードする
		HANDLE handle; // = NULL;

		if ((handle = CreateFile_Read(fileName.w_str())) == INVALID_HANDLE_VALUE) {
			return NULL;
		}

		DWORD filesize = GetFileSize(handle, NULL), readsize;
		LPSTR data = (LPSTR) Heap_Malloc(filesize);
		SetFilePointer(handle, 0, NULL, FILE_BEGIN);

		if (!ReadFile(handle, data, filesize, &readsize, NULL)) {
			CloseHandle(handle);
		}

		CloseHandle(handle);

		// ロードできる形式かどうかをチェックする
		if (spi_issupported(AnsiString(fileName).c_str(), (DWORD) data) == 0) {
			Heap_Free(data);
			continue;
		}

		// 画像を展開する
		HLOCAL info, bm;
		if (spi_getpicture(data, filesize, 1, &info, &bm, NULL, 0) != 0) {
			Heap_Free(data);
		}

		LPBITMAPINFO bmpinfo = (LPBITMAPINFO) LocalLock(info); // BITMAPINFO構造体
		LPBYTE bmbits = (LPBYTE) LocalLock(bm); // 画像データ

		// 取得した情報からBITMAPハンドルを生成する
		HDC dc = GetDC(0);
		HBITMAP bitmap = CreateDIBitmap(dc, &bmpinfo->bmiHeader, CBM_INIT, bmbits, bmpinfo, DIB_RGB_COLORS);
		ReleaseDC(0, dc);

		// Free etc...
		LocalUnlock(info);
		LocalFree(info);
		LocalUnlock(bm);
		LocalFree(bm);
		Heap_Free(data);

		return bitmap;
	}

	return NULL;
}
예제 #3
0
/**
 * @brief TLSF based PAL allocation function
 *
 * @param size Amount of memory to allocate
 *
 * @return Memory address if successful, NULL otherwise.
 */
 void *SIRF_PAL_OS_MEM_Alloc(tSIRF_UINT32 size)
{
   void *ret;
   if (SIRF_FALSE == s_mem_initialized)
   {
      return NULL;
   }

   (void)SIRF_PAL_OS_MUTEX_Enter(s_MemoryMutex);
   ret = Heap_Malloc(s_MemoryHeap, size);
   (void)SIRF_PAL_OS_MUTEX_Exit(s_MemoryMutex);

   return ret;   
}
/** 
 * Packet callback called by the sirf_ext_uart when a full packet has arrived
 * 
 * @param packet        pointer to the packet
 * @param packet_length The length of the packet
 * 
 * @return SIRF_SUCCESS if the packet was successfully processed, other error
 *         code otherwise.
 */
static tSIRF_RESULT 
SIRF_GPRS_AT_COMMAND_UartPacketCallbackATCommand( 
   tSIRF_UINT8  *packet, 
   tSIRF_UINT32  packet_length )
{
   tSIRF_UINT32 message_id;
   tSIRF_UINT32 message_length;
   tSIRF_UINT32 options;
   tSIRF_RESULT result;
   fifo_link_t *link;
   gprs_msg_t  *msg;

   options = SIRF_CODEC_OPTIONS_GET_FIRST_MSG; /* First message in the packet */
   result = SIRF_PROTO_GPRS_AT_COMMAND_Decode(packet,
                                              packet_length,
                                              &message_id,
                                              NULL, /* return just the length */
                                              &message_length,
                                              &options);

   if (SIRF_SUCCESS != result)
   {
      return result;
   }

   UTIL_Assert(0 != message_length);

   link = (fifo_link_t*) Heap_Malloc(gprs_at_command.msg_heap,
                                     sizeof(fifo_link_t) +
                                     sizeof(gprs_msg_t) + 
                                     message_length);

   if (NULL == link) 
   {
      return SIRF_GPRS_AT_COMMAND_SERVER_MEMORY_ERROR;
   }

   msg = (gprs_msg_t*)((tSIRF_UINT32)link + sizeof(fifo_link_t));
   link->data = (void*)msg;
   msg->message_structure = (void*)((tSIRF_UINT32)msg + sizeof(gprs_msg_t));

   /* actually decode the message */
   result = SIRF_PROTO_GPRS_AT_COMMAND_Decode(packet,
                                              packet_length,
                                              &msg->message_id,
                                              msg->message_structure,
                                              &msg->message_length,
                                              &options);

   if (SIRF_SUCCESS != result) 
   {
      return result;
   }

   /* Special case here that we have to switch protocols if the CONNECT message
    * comes in */
   if (SIRF_MSG_GPRS_AT_COMMAND_CONNECT == msg->message_id)
   {
      SIRF_EXT_UART_SetCallbacksFromCallback(gprs_at_command.uartno,
                                             SIRF_GPRS_AT_COMMAND_UartPacketCallbackMAS,
                                             SIRF_PROTO_MAS_Parser);
   }

   result = SIRF_PAL_OS_MUTEX_Enter( gprs_at_command.queue_mx );
   if (SIRF_SUCCESS != result) 
   {
      return result;
   }
   fifo_queue_add_tail(&gprs_at_command.msg_queue_gprs,
                       link);

   result = SIRF_PAL_OS_MUTEX_Exit( gprs_at_command.queue_mx );
   if (SIRF_SUCCESS != result) 
   {
      return result;
   }

   /* Signal a new message has arrived */
   result = SIRF_PAL_OS_SEMAPHORE_Release( gprs_at_command.semaphore );
   
   return result;
}
/** 
 * Callback to process MAS data packets
 * 
 * @param packet Packet to process
 * @param packet_length Size of the packet
 * 
 * @return SIRF_SUCCESS if successfully processed
 */
static tSIRF_RESULT SIRF_GPRS_AT_COMMAND_UartPacketCallbackMAS(
   tSIRF_UINT8* packet, tSIRF_UINT32 packet_length )
{
   tSIRF_UINT32 ii;
   tSIRF_UINT32 message_length;
   tSIRF_UINT32 message_id;
   fifo_link_t *link;
   gprs_msg_t *msg;
   tSIRF_RESULT result;
   tSIRF_UINT32 options;

   /* Search for the ETX character, and if found switch protocols immediately
    * so that no data is lost */
   for (ii = 0; ii < packet_length; ii++)
   {
      if (SIRF_PROTO_MAS_ETX == packet[ii])
      {
         /* make sure it wasn't escaped */
         if (ii == 0 || SIRF_PROTO_MAS_DLE != packet[ii-1])
         {
            SIRF_EXT_UART_SetCallbacksFromCallback(gprs_at_command.uartno,
                                                SIRF_GPRS_AT_COMMAND_UartPacketCallbackATCommand,
                                                SIRF_PROTO_GPRS_AT_COMMAND_Parser);
            /* Potential yet unlikely race condition here where we could be in the
            * middle of processing a MAS message (input or output) and ETX has come in.
            * This is OK and normal.  Sending <pause> +++ <pause> while in AT command 
            * mode will cause no harm since the modem is looking for AT<command><CR>.
            * We set the state here to try to reduce the likely hood of sending unnecessary 
            * characeters.  Also release the semaphore so the thread wakes up and
            * sets the timeout appropriately */
            gprs_at_command.state = GPRS_SERVER_STATE_AT_COMMAND;
            SIRF_PAL_OS_SEMAPHORE_Release(gprs_at_command.semaphore);            
            break;
         }
      }
   }

   /* Put the rest of the data on the queue.  If there is none, just return */
   if (0 == ii)
   {
      return SIRF_SUCCESS;
   }

   /* Find out how much memeory we need */
   result = SIRF_PROTO_MAS_Decode(packet,
                                  ii,
                                  &message_id,
                                  NULL,
                                  &message_length,
                                  &options);


   link = (fifo_link_t*) Heap_Malloc(gprs_at_command.msg_heap,
                                     sizeof(fifo_link_t) +
                                     sizeof(gprs_msg_t) + 
                                     message_length);

   if (NULL == link) 
   {
      return SIRF_GPRS_AT_COMMAND_SERVER_MEMORY_ERROR;
   }

   msg = (gprs_msg_t*)((tSIRF_UINT32)link + sizeof(fifo_link_t));
   link->data = (void*)msg;
   msg->message_structure = (void*)((tSIRF_UINT32)msg + sizeof(gprs_msg_t));

   options = SIRF_CODEC_OPTIONS_GET_FIRST_MSG; /* First message in the packet */

   /* actually decode the message */
   result = SIRF_PROTO_MAS_Decode(packet,
                                  ii,
                                  &msg->message_id,
                                  msg->message_structure,
                                  &msg->message_length,
                                  &options);

   if (SIRF_SUCCESS != result) 
   {
      return result;
   }

   msg->handle = gprs_at_command.data_handle;

   result = SIRF_PAL_OS_MUTEX_Enter( gprs_at_command.queue_mx );
   if (SIRF_SUCCESS != result) 
   {
      return result;
   }

   fifo_queue_add_tail(&gprs_at_command.listener[(tSIRF_UINT32)gprs_at_command.data_handle].msg_queue_mas,
                         link);

   result = SIRF_PAL_OS_MUTEX_Exit( gprs_at_command.queue_mx );
   if (SIRF_SUCCESS != result) 
   {
      return result;
   }

   /* Signal a new message has arrived */
   result = SIRF_PAL_OS_SEMAPHORE_Release( gprs_at_command.semaphore );
   
   return result;

}