How to get current computer's IP address in Xcode - objective-c

When testing iPhone apps that connect to my local machine's server, I need to enter my computer's local IP address as opposed to localhost in order to test with devices other than the simulator. This gets annoying with dynamic IP addresses and when multiple developers are testing. Is there a code snippet that can get the IP address of the computer that is compiling code, and NOT the IP the address of the device that the application is running on (preferably in C or Objective-C, and not Swift)?

1) Add a "Run Script" in the "Build Phases" tab of your Xcode project that contains this:
export SERVER_IP=`ipconfig getifaddr en0`
Note: change "en0" to whichever interface matches your machine. en0 is the wifi on my machine and my hard-wire is en3. Do an "ifconfig -a" in Terminal to get the list of all of your adapters and see which is which for your machine
2) Go to your project file. Click the Project itself in the left menu then Build Settings in the right side. Go to "Apple LLVM 6.0 - Custom Compiler Flags". Under "Other C Flags" -> "Debug" define a new value called -DSERVER_IP=${SERVER_IP}
This will map your build script's results into a #DEFINE in your project
3) In your code use SERVER_IP just like you would any other #DEFINE and it will always have the value of the computer that built the code.

I got this working by having a run script set the computer's IP address in the app's plist, then reading the plist value in code.
1) In your Info.plist file, add a key/value pair that will contain your computer's IP address. For this example, we'll add a key of "CompanyNameDevServerIP", of type "String". Note that this key/value pair should be prefixed uniquely, so that it doesn't conflict with Apple's keys (see here for more info).
2) In the "Build Phases" tab, add a run script that contains the following:
if [ "$CONFIGURATION" == "Debug" ]; then
echo -n ${TARGET_BUILD_DIR}/${INFOPLIST_PATH} | xargs -0 /usr/libexec/PlistBuddy -c "Set :CompanyNameDevServerIP `ipconfig getifaddr en0`"
else
echo -n ${TARGET_BUILD_DIR}/${INFOPLIST_PATH} | xargs -0 /usr/libexec/PlistBuddy -c "Delete :CompanyNameDevServerIP"
fi
This sets the computer's IP address in the plist that gets bundled with the build, but only in debug builds. (It's removed from the plist file in release builds.)
Hat tip to this blog post for providing this technique.
You may need to use a different interface other than en0 (see here for more info).
3) In code, retrieve this plist value to get your computer's IP address:
NSString *serverIP = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CompanyNameDevServerIP"];

If you need this at compile time, you can just add a "Run Script" in the "Build Phases" tab of your Xcode project. Putting this into the source code will naturally return the IP address of where the code is running, not where it was built.
This script will return the primary IP address. You can modify the script to edit a Plist or whatever you need from there. PlistBuddy works well for modifying plist files at build time.
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

Related

Raspberry Pi is not able to subscribe to ros topic published from base PC but vice-verse is working?

I am using the ubiquityrobotics Raspberry Pi image for the RPi 3B+, which is Ubuntu Xenial and ROS Kinetic. My base computer is running Ubuntu 18.04 and has ROS Melodic installed.
I created subo_base workspace in the base PC and subo_rpi workspace in the RPi (assessing the RPi via ssh).
Then I created a package in both the base PC and RPi and added the Publisher and Subscriber (http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29) files in each of the packages.
When I run the publisher from the RPi, the base PC is able to subscribe but when I publish from the base PC, the RPi does not show any output and remains stuck (even though the Topic is visible on RPi using rostopic list).
Base PC is able to subscribe to RPi
RPi unable to subscribe to topic from PC
Some of the code is used in base PC
aakash#aakash:~$ mkdir -p ~/subo_base/src
aakash#aakash:~$ cd ~/subo_base/
aakash#aakash:~/subo_base$ catkin_make
aakash#aakash:~/subo_base$ source devel/setup.bash
aakash#aakash:~/subo_base$ echo $ROS_PACKAGE_PATH
aakash#aakash:~/subo_base$ cd ~/subo_base/src/
aakash#aakash:~/subo_base/src$ catkin_create_pkg motion_plan std_msgs rospy roscpp
To connect to RPi
aakash#aakash:~/subo_base/src/motion_plan/scripts$ export ROS_MASTER_URI=http://ubiquityrobot.local:11311
aakash#aakash:~/subo_base/src/motion_plan/scripts$ export ROS_IP='hostname -I'
Further, I am able to transfer files from and to the base PC via ssh scp so I guess netwkr might not be the issue?
The issue is most likely the hostname resolution and/or ROS network variable configuration.
I dislike using the hostname in the variables, so I will give the examples using just IPs.
Also the 'hostname -I' is definitely not suitable for setting your ROS_IP variable in all cases. So that might also be one source of your problem.
From hostname man page
-I, --all-ip-addresses>
Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loop‐back interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of
the output.
You will want to use whatever specific ip address you need, so just use that or find a better way to determine which ip to set. echo $ROS_IP or printenv | grep ROS will tell you what your variables are currently set to so you can verify it is set correctly.
For minimal proof that things are working you could try the following:
Lets say your RPi ip is 192.168.0.2 and PC ip is 192.168.0.3
You will need to decide which machine will be the master, for this example I will assume the PC will be the master.
In a terminal on the PC run the following commands:
roscore
in a different terminal run (this is used instead of the subscriber/publisher node to test if things work)
rostopic pub /test/topic std_msgs/String 'Hello World from PC' -r 1
Now on the SSH terminal on the RPi run:
export ROS_MASTER_URI=http://192.168.0.3:11311 && export ROS_IP=192.168.0.2
now you should be able to echo the topic published on the PC from the SSH window.
rostopic echo /test/topic
ctrl+c out of the echo and you can try publishing some message on the RPi like:
rostopic pub /test2/topic std_msgs/String 'Hello World from RPi' -r 1
Now open a new terminal on the PC and try to echo the topic from RPi, any terminal sourced with the ROS installspace, usually source /opt/ros/kinetic/setup.bash, should work:
rostopic echo /test2/topic
ROS wiki page on running ROS on multiple machines
ROS answer regarding setting up multiple machines
ROS1
Machine1 [MASTER]:
Will run roscore, but don't run it yet till configurations are done.
Has an IP of 192.168.1.10.
1- Run the following in the terminal:
1.1- export ROS_MASTER_URI=http://192.168.1.10:11311.
1.2- export ROS_IP=192.168.1.10.
2- Now, run roscore.
Machine2 [SLAVE]
Will NOT run roscore.
Has and IP 192.168.1.15.
1- Run the following in the terminal:
1.1- export ROS_MASTER_URI=http://192.168.1.10:11311.
1.2- export ROS_IP=192.168.1.15.
2- Now, you are connected to the Master.
ROS2
ROS2 will populate the information on the LAN out of the box without any configuration.

ls doesn't show any output in ssh connected to an Debian 9 VM instance in Google Cloud Platform

this might be a dumb question, but I checked everywhere and there's no direct answer to it.
I set up both SSH keys successfully and I can connect to my instance via terminal, but when I do "ls", it doesn't show me any output. I am using iTerm2 with zsh on my Mac but I don't think this is an issue.
Can anybody give me a hint? Thanks!
When you access a VM through SSH, your working directory is the home directory of the user specified with the SSH command, i.e. /home/username. In case you access as root, the working directory will be /root.
You can check it through the command pwd
If it is a brand new machine, it is normal that the output of 'ls' is empty since in your home directory no file matches the filters of 'ls' with no parameters. The reason is that 'ls' doesn't show filenames starting with a dot ('.') because in the Linux convention they are hidden unless you run ls -al.
You can try again with $ ls -al and you will be able to see hidden files and directories as well.
On the other hand you can create as well first an empty file and then running again 'ls':
$ touch file
$ ls

How to set CURRENT_PROJECT_VERSION in Xcode 8

Since upgrading to Xcode 8, when I do a build with fastlane, I get the following error message:
There does not seem to be a CURRENT_PROJECT_VERSION key set for this project
If I go to Xcode > Build Settings and go down to Versioning, there is a Current Project Version key, as shown below:
The help text says to enter an integer or floating point number, but when I click on the field, there is no opportunity to enter a number in either the Debug or Release field. This is different from the screen shot shown in this apple tech Q&A so there appears to have been a change in Xcode since the Q&A was released.
Don't. Modify the values in your app's info.plist file instead.
This means not using agvtool (as I learned).
Why? Over the years, Apple has come up with several manners of changing version and build numbers. Many of them are now outdated and poor practice. Changing CURRENT_PROJECT_VERSION modifies values within your project's project.pbxproj file and if you are running a distributed team, this will cause merge conflicts if the other half of the team tries to update and while they were asleep, you updated this internal value. If you are using pods, you'll get several more merge conflicts per pod that you add to the project.
So, CURRENT_PROJECT_VERSION?
Don't use it.
Within the info.plist file are these keys.
CFBundleVersion
CFBundleShortVersionString
Use CFBundleVersion for your app's build number.
Use CFBundleShortVersionString for your app's version number.
Use Plistbuddy to do it.
<key>CFBundleShortVersionString</key>
<string>3.0.7</string>
<key>CFBundleVersion</key>
<string>934</string>
</dict>
</plist>
Try the script below.
#!/bin/sh
# To make executable, use: chmod u+x Build-Versioning-Scripts/Increment_Build_Number.sh
# to locate your target's info.plist use
# ${PRODUCT_SETTINGS_PATH}
echo "----"
echo "Info.plist for target: ${PRODUCT_SETTINGS_PATH}"
buildNum=$(/usr/libexec/Plistbuddy -c "Print CFBundleVersion" "${PRODUCT_SETTINGS_PATH}")
echo "Current build #: $buildNum"
if [ -z "$buildNum" ]; then
echo "No build number found in $PRODUCT_SETTINGS_PATH"
exit 2
fi
buildNum=$(expr $buildNum + 1)
echo "Build # incremented to: $buildNum"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNum" "$PRODUCT_SETTINGS_PATH"
echo "----"
exit 0
By adding this script to your build or archive process on your build machine, this will automatically update the app's build number.
If you wish to increment your app's version number, change CFBundleShortVersionString (Bundle versions string, short) in the info.plist manually.
Currently when you get that fastlane error, terminal logs redirects you to
Automating Version and Build Numbers Using agvtool
to understand what you need to do.
Summary
Enable agvtool.
Build Settings > Current Project Version > $(CURRENT_PROJECT_VERSION)
Build Settings > Versioning System > Apple Generic
Set up your version and build numbers.
Target > Info > Bundle versions string, short (CFBundleShortVersionString) > "your init
version"
Target > Info > Bundle version (CFBundleVersion) > "your
init value"
That helps me a lot.
you may try change project format ?
I suggest you can change this xcode format version.

MSP430 toolchain in linux

Can anybody please guide procedure to setup tool-chain for MSP430 in Linux (particularly Ubuntu) ? I am using MSP430 launchpad (MSP-EXP430G2), and I need to setup compiler/build tools and debugger drivers.
If you install Texas Instruments' CCS IDE, Linux version, it will install the tool-chain. There are, however, other problems in developing for MSP430 in Linux. The bugs and fixes are detailed in my post here:
MSP430 / eZ430-RF2500 Linux support Guide
"Compile code using Code Composer Studio (CCS)
Download CCS for Linux.
Create a new CCS project with a Custom MSP430 Device or any other.
Compile the code. The result binary image will be in the workspace. The workspace path can be found in “File” / “Switch workspace”.
The file that should be programmed to the device is the project-name.out file.
Program and run device using mspdebug
Download and Install mspdebug
From the directory with the file project-name.out run:
$ sudo mspdebug rf2500
Now you are in mspdebug’s command line shell. Run the following to program and run the device:
(mspdebug) prog project-name.out
(mspdebug) run
Use Ctrl+c to pause run and get command line back.
Fix a Linux Kernel bug that prevents Minicom to communicate with device
The device path in /dev is /dev/ttyACM0. Currently, connecting to it
serially using utilities such as minicom is not possible, and you get the message “/dev/ttyACM0: No such file or directory”.
The bug is in Kernel module “cdc_acm”. The solution is to fix the bug in the source code, recompile the module and plug it instead of the existing one.
Find out Linux version:
$ uname -r
cdc_acm’s source is the files cdc-acm.c and cdc-acm.h. They are under the Linux path drivers/usb/class/.
Download these two files from a repository that matches your Linux version. Such repos are available in lxr.free-electrons.com and www.kernel.org.
Create a new directory and move the files to it.There are two code segments need to be removed or commented out:
The next lines appear in function “acm_port_activate()” on newer versions and in “acm_tty_open()” in older ones:
// if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
// (acm->ctrl_caps & USB_CDC_CAP_LINE))
// goto bail_out;
The next line appears in function “acm_port_shutdown()” on newer versions “acm_port_down()” in older ones:
// acm_set_control(acm, acm->ctrlout = 0);
Create a Makefile and compile:
$ echo 'obj-m += cdc-acm.o' > Makefile
$ make -C /lib/modules/`uname -r`/build M=$PWD modules
You should have a new cdc-acm.ko file in the directory
Replace the existing module (This change will be discarded after boot):
$ sudo rmmod cdc-acm
$ sudo insmod ./cdc-acm.ko
Communicate via the serial port using Minicom
Launch minicom setup from command line:
$ minicom -s
In the menu, choose:
Serial port setup
Press ‘A’ (for “Serial Device”).
Replace Current device path with:
/dev/ttyACM0
Press ‘E’ (for “Bps/Par/Bits”).
Set the correct data rate for your device.To lower the rate (to 1200, for instance), keep pressing ‘B’ (for “previous”) until the top line shows:
Current: 1200 8N1
Press “Enter” until returning to main menu, there, press “Exit”.This will exit the setup menu and start running on the device. From now on you should see messages over the serial connection: It is up to you to program the device with such messages."
Download the pre-compile tool-chain (.run file) form http://www.ti.com/tool/msp430-gcc-opensource
Unzip
Execute chmod +x <downloaded file>
Run the installer
enjoy!

ios Symbolication Server side

How to symbolicate the ios crash report after uploading to server in a linux environment where iOS development tools and scripts will not be available. I know Apple uses atos and some other tools to map the hex addresses to symbol along with .dYSM file.
I can upload .dYSM file along with crash report to server. Refered QuincyKit, but they are doing symbolication locally. But other's like HockeyApp and Critterism are doing it remotely.
Pls recommend the possible ways to do it in server.
It is possible. You can take a look at https://github.com/facebook/atosl
I got it working under Linux. (Ubuntu Server) However, it takes some time to get it up and running.
Installing atosl
First, you need to install libdwarf-dev, dwarfdump, binutils-dev and libiberty-dev.
E.g. on Ubuntu:
$ sudo apt-get install libdwarf-dev dwarfdump binutils-dev libiberty-dev
Download or clone the atosl repo from GitHub:
$ git clone https://github.com/facebook/atosl.git
CD to the atosl dir
$ cd atosl
Create a local config config.mk.local which contains a flag with the location of your binutil apps. (in Ubuntu by default that's /usr/bin). If you're not sure, you can find out by executing cat /var/lib/dpkg/info/binutils.list | less and copy the path of the file objdump. E.g. if the entry is /usr/bin/objdump, your path is /usr/bin.
So in the end, your config.mk.local should look like this:
LDFLAGS += -L/usr/bin
Compile it:
$ make
Now you can start using it:
$ ./atosl --help
Symbolicating example
To show how atosl is used, I'll provide a simple example.
Now let's take a look at a line from the crash log:
13 ErrorApp 0x000ea294 0xe3000 + 29332
To symbolicate this, we will need the load address, and the runtime address.
In this example the runtime address is 0x000ea294, and the load address is 0xe3000.
Now we have everything we need:
$ ./atosl -o [YOUR_dSYM_FILE] -l [LOAD_ADDRESS] [RUNTIME_ADDRESS]
In this example:
$ ./atosl -o ErrorApp.app.dSYM/Contents/Resources/DWARF/ErrorApp -l 0xe3000 0x000ea294
Which returns the symbolicated line:
main (in ErrorApp) (main.m:16)
FYI
Your vmaddr, which usually is 0x00001000, you can find by looking at the segname __TEXT Mach-O load command of your binary. In my example, this happens to be different, namely 0x00004000
To find the address, we need to do some math.
The address is found by the following formula:
address = vmaddr + ( runtime_address - load_address )
In this example our address is:
0x00004000 + ( 0x000ea294 - 0xe3000 ) = 0xB294
I haven't played around with this that much yet, but for now it seems to give me the results I needed. Maybe it will work for you too.
You need to implement your own linux compatible versions of atos, otool and dwarfdump (at least the functionality needed for symbolication). The Apple tools are not open source and only run on Mac OS X.
None of the services provide a solution that can be used by 3rd parties on non OS X systems. So your only chance, besides implementing the required functionality to run on your linux system, is to do it on a Mac like QuincyKit does it, see https://github.com/TheRealKerni/QuincyKit/wiki/Remote-symbolication or use a third party service.
Note: I am the creator of QuincyKit and Co-Founder of HockeyApp.