Sunday, July 24, 2011

_CRT_SECURE_NO_WARNINGS Error Message in Visual Studio 2010 (VC++)

Using "legacy"/"unsecure" I/O functions in Visual Studio 2010 would trigger error similar to this:

1>emufuncs.cpp(672): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\stdio.h(234) : see declaration of 'fopen'


which results in this error:

1>peutils.cpp(64): error C3861: 'dont_use_fgetc': identifier not found
1>peutils.cpp(224): error C3861: 'dont_use_fseek': identifier not found
1>peutils.cpp(229): error C3861: 'dont_use_fread': identifier not found
1>peutils.cpp(247): error C3861: 'dont_use_ftell': identifier not found

If you insist on using the "legacy" code, the solution is to define the following preprocessor definitions in your Visual Studio 2010 project properties:

USE_STANDARD_FILE_FUNCTIONS;_CRT_SECURE_NO_WARNINGS;

The preprocessor definition can be accessed in this menu: Project|Properties(Alt+F7)|Configuration Properties|C/C++|Preprocessor|Preprocessor Definitions.

After defining USE_STANDARD_FILE_FUNCTIONS;_CRT_SECURE_NO_WARNINGS; in your Visual C++ project, you should be good to go.

Happy coding.

Sunday, June 5, 2011

Visual Studio 2010 Code Editor Bug (Applies to C++ Code Editing)

Visual Studio 2010 Code Editor has stability issues in several platforms (mine is Windows 2008 Server SP2 - x86) as described here. In that description, the guy was having a slow response in IntelliSense.

Unfortunately for me, I was not only experiencing the lag, but also frequent crashes which was very irritating. The cause is a dependency on Windows Automation API.

The Bug Fix can be downloaded at http://support.microsoft.com/kb/971513/. Apparently, Visual Studio 2010 has dependency on Windows Automation API v3.0 which didn't ship with Windows Server 2008 SP2 or Windows Vista and previous Microsoft OSes.

Hopefully, this helps poor souls out there who gets irritated during C++ development in VS2010.

Goodluck :-)

Monday, March 7, 2011

Developing Software for Embedded Systems

Developing software for embedded system is particularly challenging. Therefore you have to make sure:

1. You know how to get debugging messages out of the system during development. Otherwise countless nightmare will wait for you.

2. Always place a "failsafe" code inside your brand new code so that the system would always recover to a point where you would be able to restore the system back to previously known working condition.

Now, let me explain point 1. Because not all embedded system contains a display, the debugging messages usually forwarded to an UART. The reason is simple, UART is a very simple digital device to initialize. In present day x86 systems, UART usually resides in the SuperIO chip or in the southbridge chipset. A display is far more difficult to initialize compareed to UART and sometimes does not available in the target system. By using an UART, it is also easier to do debugging once the system is deployed.

Let's move to point 2. In some systems, you have to remove some sort of flash chip and flash it somewhere else to recover from a bad software. Therefore, in order to reduce the hassle, it's better to provide "failsafe" code whenever possible.

Saturday, October 23, 2010

Samsung Galaxy 5 USB Tethering in Slackware64 Linux with Pppd

Samsung Galaxy 5 (Android 2.1 Eclair) is different from other Android phone in the Galaxy lines of products. It doesn't act like a router by default like its more capable brethren, the Samsung Galaxy S. The problem with setting up this device in Linux is just like the Nokia E61
that I elaborated here a few years ago. That is: it has a vendor-specific AT command set to set up the device as a modem.

The following is the summary of the steps to use the Galaxy 5 as a USB modem:

  1. Disable "automatic data connection" on the Galaxy 5. Deactivate: Settings | Wireless & networks| Mobile networks | Enable always-on mobile. Activating always-on mobile option will force the phone to use data connection (GPRS/EDGE/3G/HSDPA/HSUPA) whenever no WLAN coverage could be used. In that state, the phone acts as the endpoint of the data connection (usually a PPP connection). Because a PPP login account cannot be used by more than one device at a time in the same network, the PPP connection directly from the phone would effectively "disable" new PPP connection from a computer which uses the phone as a modem to connect. That's why you have to deactivate the always-on mobile option.

  2. Prepare the configuration files for Galaxy 5

  3. Watch syslog message in your PC/Laptop using the tail -f /var/log/message command

  4. Connect the Galaxy 5 to your PC/Laptop with the USB cable

  5. Choose Kies mode when Galaxy 5 ask for the operating mode

  6. Syslog will display the phone as one of the tty device. In my case the device is ttyACM0

  7. Run Pppd by using the tty device in the previous step as the tty device



Among the steps above, the ones which need further explanation are the first and the last steps.

The ISP used in this example is represented by the "indosatm2" string in all of the configuration files.


The configuration file that I use as follows.

The /etc/ppp/options is: (disabled options are not shown)

# /etc/ppp/options
#
# $Id: options,v 1.4 1996/05/01 18:57:04 alvar Exp $
#
# Originally created by Jim Knoble
# Modified for Debian by alvar Bray
# Modified for PPP Server setup by Christoph Lameter
# Modified for Slackware by Pat Volkerding
#
# Use the command egrep -v '#|^ *$' /etc/ppp/options to quickly see what
# options are active in this file.

asyncmap 0
crtscts
lock
modem
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4


The "peers" file (/etc/ppp/peers/im2_galaxy5) used to call the ISP as follows:

/dev/ttyACM0 921600 crtscts
connect 'chat -v -f /etc/ppp/chat/im2_galaxy5_connect_chat'
disconnect 'chat -v -f /etc/ppp/chat/im2_galaxy5_disconnect_chat'
defaultroute name your_login debug noipdefault usepeerdns


The "connect" chat file (/etc/ppp/chat/im2_galaxy5_connect_chat) as follows:

ECHO ON
ABORT 'NO CARRIER'
ABORT 'NO DIALTONE'
ABORT 'ERROR'
ABORT 'NO ANSWER'
ABORT 'BUSY'
'' ATZ
OK 'ATE0V1&D2&C1S0=0'
OK 'ATS7=60'
OK 'ATS0=0'
'' ATZ
OK 'ATE0V1&D2&C1S0=0'
OK 'ATS7=60'
OK 'AT+CGDCONT=1,"IP","indosatm2","",0,0'
OK 'ATDT*99#'
CONNECT
~ ''


The "disconnect" chat file (/etc/ppp/chat/im2_galaxy5_disconnect_chat) as follows:

ECHO ON
ABORT 'BUSY'
ABORT 'ERROR'
ABORT 'NO DIALTONE'
SAY "\nSending break to the modem\n"
"" "\K"
"" "\K"
"" "\K"
"" "\d\d+++\d\dATH"
SAY "\nPDP context detached\n"


The "connect" chat file is used to setup the Galaxy 5 (modem) to connect to the ISP while the "disconnect" chat file is used to "reset" the Galaxy 5 (modem) after the connection is closed.

The PAP secret file (/etc/ppp/pap-secrets) as follows:

# Secrets for authentication using PAP
# client server secret IP addresses
your_login * your_password *



A Password BUG in Pppd v2.4.4 caused the PAP authentication to fail when your password contains the "@" character and you're using Samsung Galaxy 5. Eventhough the "@" is escaped with "\" in the "secret" column shown above, it will still fail to authenticate properly. The message in Syslog says the PAP authentication is successful. However, that's not exactly the case because the IPCP negotiation always failed. Though, other cellphone probably works just fine with Pppd 2.4.4.


Perhaps you're asking: where did I found the Samsung Galaxy 5-specific AT command-set shown in the chat file above. I found it in the modem log file in Windows when running tethering with Kies. This is the log file in C:\Windows\ModemLog_SAMSUNG Android USB Modem.txt

10-23-2010 03:18:58.312 - File: C:\WINDOWS\system32\tapisrv.dll, Version 5.1.2600
10-23-2010 03:18:58.312 - File: C:\WINDOWS\system32\unimdm.tsp, Version 5.1.2600
10-23-2010 03:18:58.312 - File: C:\WINDOWS\system32\unimdmat.dll, Version 5.1.2600
10-23-2010 03:18:58.312 - File: C:\WINDOWS\system32\uniplat.dll, Version 5.1.2600
10-23-2010 03:18:58.328 - File: C:\WINDOWS\system32\drivers\modem.sys, Version 5.1.2600
10-23-2010 03:18:58.328 - File: C:\WINDOWS\system32\modemui.dll, Version 5.1.2600
10-23-2010 03:18:58.328 - File: C:\WINDOWS\system32\mdminst.dll, Version 5.1.2600
10-23-2010 03:18:58.328 - Modem type: SAMSUNG Android USB Modem
10-23-2010 03:18:58.328 - Modem inf path: oem30.inf
10-23-2010 03:18:58.328 - Modem inf section: SAMSUNG.Install
10-23-2010 03:18:58.328 - Matching hardware ID: usb\vid_04e8&pid_689e&mi_00&os_nt
10-23-2010 03:18:58.343 - 921600,8,N,1, ctsfl=0, rtsctl=1
10-23-2010 03:18:58.343 - Initializing modem.
10-23-2010 03:18:58.343 - DSR is low while initializing the modem. Verify modem is turned on.
10-23-2010 03:18:58.343 - CD is high while initializing modem.
10-23-2010 03:18:58.359 - Send: AT
10-23-2010 03:18:58.359 - Recv: AT
10-23-2010 03:18:58.359 - Command Echo
10-23-2010 03:18:58.359 - Recv: OK
10-23-2010 03:18:58.359 - Interpreted response: OK
10-23-2010 03:18:58.359 - Send: ATE0V1&D2&C1S0=0
10-23-2010 03:18:58.359 - Recv: ATE0V1&D2&C1S0=0
10-23-2010 03:18:58.359 - Command Echo
10-23-2010 03:18:58.359 - Recv: OK
10-23-2010 03:18:58.359 - Interpreted response: OK
10-23-2010 03:18:58.375 - Send: ATS7=60
10-23-2010 03:18:58.375 - Recv: OK
10-23-2010 03:18:58.375 - Interpreted response: OK
10-23-2010 03:18:58.375 - Waiting for a call.
10-23-2010 03:18:58.390 - Send: ATS0=0
10-23-2010 03:18:58.390 - Recv: OK
10-23-2010 03:18:58.390 - Interpreted response: OK
10-23-2010 03:18:58.390 - 921600,8,N,1, ctsfl=1, rtsctl=2
10-23-2010 03:18:58.390 - Initializing modem.
10-23-2010 03:18:58.390 - DSR is low while initializing the modem. Verify modem is turned on.
10-23-2010 03:18:58.390 - CD is high while initializing modem.
10-23-2010 03:18:58.406 - Send: AT
10-23-2010 03:18:58.406 - Recv: OK
10-23-2010 03:18:58.406 - Interpreted response: OK
10-23-2010 03:18:58.406 - Send: ATE0V1&D2&C1S0=0
10-23-2010 03:18:58.406 - Recv: OK
10-23-2010 03:18:58.406 - Interpreted response: OK
10-23-2010 03:18:58.421 - Send: ATS7=60
10-23-2010 03:18:58.421 - Recv: OK
10-23-2010 03:18:58.421 - Interpreted response: OK
10-23-2010 03:18:58.421 - Dialing.
10-23-2010 03:18:58.437 - Send: ATDT*###
10-23-2010 03:19:01.468 - Recv: CONNECT 115200
10-23-2010 03:19:01.468 - Interpreted response: Connect
10-23-2010 03:19:01.468 - Connection established at 115200bps.
10-23-2010 03:19:01.468 - Error-control off or unknown.
10-23-2010 03:19:01.468 - Data compression off or unknown.
10-23-2010 03:19:31.468 - Read: Total: 3711, Per/Sec: 122, Written: Total: 3553, Per/Sec: 117
10-23-2010 03:21:31.468 - Read: Total: 994835, Per/Sec: 8259, Written: Total: 83780, Per/Sec: 668


A note about the the chat configuration file

Anyway, perhaps what is not so clear in the chat file is the "~" character in the last line. It actually tells the chat script to expect a "~" (tilde) as a response after the
serial connection with the remote PPP endpoint is established.

The following excerpt from PPP-HOWTO (http://www.linux.org/docs/ldp/howto/PPP-HOWTO/x419.html)
explain about it.


8.3.3. The remote PPP process is slow to start

This one is a bit tricksy!

By default, your Linux pppd is compiled to send a maximum of 10 lcp configuration requests. If the server is a bit slow to start up, all 10 such requests can be sent before the remote PPP is ready to receive them.

On your machine, pppd sees all 10 requests reflected back (with the 8th bit stripped) and exits.

There are two ways round this:-

Add "lcp-max-configure 30" to your ppp options. This increases the maximum number of lcp configure packets pppd sends before giving up. For really slow server, you may need even more than this.

Alternatively, you can get a bit tricksy in return. You may have noticed that when you logged in by hand to the PPP server and PPP started there, the first character of the ppp garbage that appears was always the tilde character (˜).

Using this knowledge we can add a new expect/send pair to the end of the chat script which expects a tilde and sends nothing. This would look like:-

\~ ''

Note: as the tilde character has a special meaning in the shell, it must be escaped (and hence the leading backslash).


Another thing in the chat script is: Samsung Galaxy 5 will emit a "CONNECT" string when the serial connection
is established with the remote endpoint.