📄 commprotocol_md.c
字号:
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2001
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* commProtocol_md.c
*
* Project:
* --------
* Maui_Software
*
* Description:
* ------------
* This file implements comm port JVM native codes
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
/*************************************************************************
* Include Header Files
*************************************************************************/
#include "jal.h"
#include "jam_internal.h"
#include "jvm_internal.h"
#include "commProtocol.h"
#include "uart_sw.h"
/*************************************************************************
* Protocol implementation functions
*************************************************************************/
void
COMM_ReadInd_Handler(int port)
{
}
void
COMM_WriteInd_Handler(int port)
{
if(port == PS_UART_PORT)
{
kal_set_eg_events(J2ME_comm_eventgroup, 2, KAL_OR);
}
}
/*=========================================================================
* FUNCTION: freePortError
* OVERVIEW:
* INTERFACE:
* parameters: *pszError: Error message
* returns: <nothing>
*=======================================================================*/
void
freePortError(char* pszError)
{
pszError = NULL;
}
/*=========================================================================
* FUNCTION: openPortByNumber
* OVERVIEW: Opens a serial port from the specified port number.
* INTERFACE:
* parameters: **ppszError: Error message
* port: Port number
* baudRate: The speed of the connection.
* options: Options to be used for the port.
* returns: The open port
*=======================================================================*/
long
openPortByNumber(char** ppszError, long port, long baudRate, long options)
{
long hPort;
if ((hPort = jam_comm_open(port, baudRate, options)) < 0) {
*ppszError = getErrorMsg(MIDP_COMMPROTOCOL_ERROR,
MIDP_COMMPROTOCOL_OPENPORT_ERROR);
return(-1);
}
return hPort;
}
/*=========================================================================
* FUNCTION: configurePort
* OVERVIEW: Configures the open serial port.
* INTERFACE:
* parameters: **ppszError: Error message
* hPort: The port to be configured.
* baudRate: The speed of the connection.
* options: Options to be used for the port.
* returns: <nothing>
*=======================================================================*/
void
configurePort(char** ppszError, int hPort, long baudRate, unsigned long options)
{
jam_comm_config(hPort, baudRate, options);
}
/*=========================================================================
* FUNCTION: closePort
* OVERVIEW: Closes the open serial port.
* INTERFACE:
* parameters: hPort: The open port.
* returns: <nothing>
*=======================================================================*/
void
closePort(long hPort)
{
/* Only TCK use comm port, and rely on resource manager to restore setting
* Or uart will assert failed.*/
commCloseHandle(hPort);
if (Java_State == JAVA_ACTIVATED_STATE)
resouceDereg(JAVA_IO_COMM_DEVICE, hPort);
}
/*=========================================================================
* FUNCTION: writeToPort
* OVERVIEW: Writes to the open port, without blocking
* INTERFACE:
* parameters: **ppszError: Error message
* hPort: The name of the port to write to.
* *pBuffer: The data to be written to the port.
* nNumberOfBytesToWrite: The number of bytes to write.
* returns: The number of bytes written.
*=======================================================================*/
long
writeToPort(char** ppszError, long hPort, char* pBuffer, long nNumberOfBytesToWrite)
{
if(hPort == PS_UART_PORT)
{
int i = 0;
kal_uint16 total_send = 0;
kal_uint32 event_group;
while (total_send < nNumberOfBytesToWrite) {
i = jam_comm_putbytes(hPort, (kal_uint8*)(&pBuffer[total_send]),
(kal_uint16)(nNumberOfBytesToWrite-total_send));
if(i < 0)
return -1;
total_send += i;
if (0 < (nNumberOfBytesToWrite-total_send)) {
kal_retrieve_eg_events(J2ME_comm_eventgroup, 2, KAL_OR_CONSUME
, &event_group, KAL_SUSPEND);
}
}
return(long)total_send;
}
else
{
int i = 0;
i = jam_comm_putbytes(hPort, (kal_uint8*)pBuffer,
(kal_uint16)(nNumberOfBytesToWrite));
if(i < 0)
return -1;
return(long)i;
}
}
/*=========================================================================
* FUNCTION: readFromPort
* OVERVIEW: reads from a serial port, without blocking
* INTERFACE:
* parameters: **ppszError: Error message
* hPort: The name of the port to read from.
* pBuffer: The buffer to store the bytes read.
* nNumberOfBytesToRead: The number of bytes to read.
* returns: The number of bytes read.
*=======================================================================*/
long
readFromPort(char** ppszError,long hPort,char* pBuffer,long nNumberOfBytesToRead)
{
if(hPort == PS_UART_PORT)
{
long total = 0, recv = 0;
int loop_count = 0;
while (total < nNumberOfBytesToRead) {
recv = jam_comm_getbytes(hPort, (kal_uint8*)&pBuffer[total],
(kal_uint16)(nNumberOfBytesToRead-total));
if(recv < 0)
return -1;
total += recv;
if (recv == 0 && loop_count++ >= 20) break;
kal_sleep_task(5);
}
*ppszError=NULL;
return total;
}
else
{
long recv = 0;
recv = jam_comm_getbytes(hPort, (kal_uint8*)pBuffer,
(kal_uint16)(nNumberOfBytesToRead));
if(recv < 0)
return -1;
*ppszError=NULL;
return recv;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -