tag:blogger.com,1999:blog-23492010291142508172024-03-05T11:37:58.069-08:00S.Word Random posts of arbitrary passion. dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.comBlogger196125tag:blogger.com,1999:blog-2349201029114250817.post-11530296799045187212022-01-30T17:15:00.001-08:002022-01-30T17:15:15.886-08:00Ethics and AI: How Human flaws translate to Artificial Intelligence?<p><strong style="text-align: justify;"><span lang="EN-US" style="color: #0e101a;"><span style="font-family: arial;">“Two things are infinite: the universe and human stupidity; and I’m not sure about the universe!”</span></span></strong></p><p style="margin: 0cm; text-align: justify;"><span lang="EN-US" style="color: #0e101a;"><span style="font-family: arial;">Humans are perfect and human behavior is impeccable. This claim is not true by any stretch of the imagination. With a population bulging over 7 billion, history of wars, political and geographical differences of varying proportions, virtually uncountable religious and cultural beliefs and the inherent complexity of the human psyche; it is safe to say that human behavior is equally difficult to model as it is far from the ideal. Almost any action being undertaken by man throughout the globe has an inevitable uncertainty of outcome as humans are bound to make mistakes. The fundamental purpose of machine automation is to eliminate this very inconsistency and inefficiency associated with humans. Dealing with things like inconsistency and inefficiency is easy with machines that function in closed environments. But there is a whole other aspect to human limitations. There are decisions that humans can’t make; not because of our biological shortcomings but rather due to the sheer scope of the implications of such decisions. And such decisions are not obscure at all. We face many situations in our day to day life where our actions (or lack of any) can potentially lead to serious consequences but we continue to be arbitrarily irresponsible out of moral and/or mechanical incompetence. This, unfortunately, is the accepted state of mankind but <strong>what worries me a lot is the continuous process of</strong> <strong>giving control over to autonomous machines</strong> <strong>for such</strong> <strong>delicate and potentially disastrous scenarios</strong>.<o:p></o:p></span></span></p><p style="margin: 0cm; text-align: justify;"><span lang="EN-US" style="color: #0e101a;"><span style="font-family: arial;">A common discussion in most AI ethics workshops (including this one) is around the hypothetical and menacing situation of a train on course to kill either a group of 5 people or a single person and the decision to choose between 1 death and 5 is left to the audience by virtue of a button which changes the train’s course. There are other variations that offer a choice between the life of a toddler and an old man. All these scenarios offer the same fundamental question of <strong>can we quantify or evaluate the life or death of anyone</strong> like that? But this question is still just on the surface. The much more important and hidden question is <strong>should we quantify or evaluate human life or death? </strong>Sure, to some people (including myself), letting 5 people or a toddler die is much worse than letting a single person or an old man die. But a single person does not have the right and therefore, should not have the power to make such a decision. This example is popular because it leads directly into the realm of self-driving cars. <strong>I have realized that designing a self-driving car, for example, is not just a matter of building a system intelligent enough to handle all the physical and mechanical intricacies of the road. It must also include ways to handle misfortunate situations where human lives are at risk.</strong> And that is where I feel there is no right approach. A human shouldn’t decide which person to kill (by letting the car drive over in case of an accident) and so shouldn’t any AI system created by humans.<o:p></o:p></span></span></p><p style="margin: 0cm; text-align: justify;"><span lang="EN-US" style="color: #0e101a;"><span style="font-family: arial;">Another discussion covered in the workshop was about the various types of bias induced in autonomous and intelligent software. The basis of many of these biases was rooted in human discrimination. As mentioned previously, our perception of the world is far from ideal. There is a tremendous imbalance of power and influence in human society. <strong>Power corrupts and absolute power corrupts absolutely</strong>. People, corporations and even governments do not always act for the common good. Their actions are sometimes motivated by greed (wrong use of data collection), misguided by bias (systems used by police) or ignorant of the long-term effects (a unanimous shift towards autonomous weapons). And unfortunately, the fate of the majority of AI is in their hands. So much of software development is incremental and if corporations or governments continue to churn out software that is not transparent and fair this can lead to software gaining not only in intelligence and influence but also in secrecy and malintent. This is a dangerous cycle that is only gaining momentum and it needs to be corrected.<o:p></o:p></span></span></p><p style="margin: 0cm; text-align: justify;"><span lang="EN-US" style="color: #0e101a;"><span style="font-family: arial;">Mimicking human judgment is hard enough. It may even be impossible. But one thing is for sure, there are situations where even humans can’t comprehend all implications of an action and as such, we cannot expect any AI in the same situation to do the “right thing”. In these impossible situations, both humans and AI will be out of options but with a more potent and ruthless AI, the scale of destruction will be much greater. Therefore, it is important to not give absolute power to AI in such cases. And then there are situations where I feel it is imperative to separate AI from human bias, greed, and corruption. History suggests that we are excellent in self-inflicted damage. So, as mankind is marching forward in the creation of things that are potentially more capable than humans then we must be very careful that we do not end up with things that do more damage than good.</span><span style="font-family: Calibri, sans-serif;"><o:p></o:p></span></span></p>dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-65685125912657631922022-01-30T17:11:00.003-08:002022-01-30T17:11:37.618-08:00Ethics and AI: The Charade of Privacy<p><span style="font-family: Calibri, sans-serif; font-size: 12pt; text-align: justify;">It is very difficult to put a definitive status on privacy in today’s digital age. I am not sure if privacy is completely dead or if it will die in a certain period of time. But one thing I am quite confident in is that privacy is dying and it is dying fast. The diminishing privacy is not a consequence of a single phenomenon. It is in fact, caused by the combination of a variety of things. Ever increasing volume of data being generated, computer algorithms becoming abundantly sophisticated and humans growing in eagerness to share every aspect of their lives with the world; these to me are the primary factors that are proving to be fatal to the concept of privacy as we know it.</span></p><p class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 15.693333625793457px; margin: 0cm 0cm 8pt; text-align: justify;"><span lang="EN-US" style="font-size: 12pt; line-height: 17.1200008392334px;">The very first thing we need to consider is the near unfathomable size of data being produced by a single human on average. As per the statistic given in the key note speech at <b>2017 CeBIT Global Conference</b> by <b>Dr. Michal Kosinski</b>, a single human produced <b>500 MB</b> of data per day. This statistic is over two years old and therefore this number must have increased by factors already. Not all of this data is produced intentionally by humans. In fact, most of it is gathered silently by companies governing the Internet. “<b><i>If you are not paying for a service, you are the product</i></b>.” This quote sums up the situation perfectly. If companies on the Internet are incentivizing the end of human privacy then us humans are sponsoring it by giving away the perfect resource to extract out every little detail there is to extract from our lives. For me, the problem is not that data is being collected in various manners from our digital footprint, but it is the fact that we are so careless in our choices when it comes to data and so many of us are keen on voluntarily broadcasting their lives to the world. We are actively contributing to this ocean of data that is basically an essence of our online existence. For example, it is one thing that our location data is being recorded by Google, but what can be made of the pictures we share on Instagram, blatantly showing off where we are and what we are doing. Companies and the government are data hungry and humans are so full of themselves that they can’t stand the thought of being unnoticed. And this creates a recipe of disaster for anonymity on the Internet.<o:p></o:p></span></p><p class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 15.693333625793457px; margin: 0cm 0cm 8pt; text-align: justify;"><span lang="EN-US" style="font-size: 12pt; line-height: 17.1200008392334px;">Artificial Intelligence is burgeoning and algorithms are becoming smarter, quicker and more efficient in the way in which they make use of the information available to them. Algorithms today don’t need a complete and connected dataset pertaining to a person in order piece together a decent analysis. Even if there are attempts of scrambling sensitive information in order to lose meaning in the shuffle, computers are pretty good at recognizing even the feeblest of patterns and eventually singling out humans from these patterns. One example of how efficient machine learning techniques are at extrapolating personality trait from a very basic set of data is given in a study that used <b>Facebook likes</b> of few million people and predicted things like <b>sexual orientation, political views and general personality</b>. The study showed that with a few hundred likes, the algorithm does a better job at predicting a person’s behavior than all of that person’s friends, family and spouse. It is established that keeping secrets from family and spouse is never easy, then how can a person’s privacy be kept from such algorithms that know more with so little amounts of data? The simple answer is that it is impossible to maintain privacy specially with amount of access we have allowed in our lives. The fact of the matter is that cracking privacy is like finding patterns and computers are pretty good at cracking patterns specially when we are not making it hard for computers by providing as much data as they like.<o:p></o:p></span></p><p class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 15.693333625793457px; margin: 0cm 0cm 8pt; text-align: justify;"><span lang="EN-US" style="font-size: 12pt; line-height: 17.1200008392334px;">Attempting to protect privacy in my opinion is a lost cause. All the companies that employ user data need to be regulated. But how can we have hope when the regulator i.e. the government itself is big on gathering data from people and using it to control us. To be honest, I don’t see much harm in all of this. I am not complaining about the lack of privacy, I am just stating its inevitability. For me, privacy is relevant with respect to other people. As long data is being kept from people in a person’s social circle, it is fine if some corporation uses it further their business. Afterall, the overall goal is to better server the consumer and if that’s the case then I see no problems with our behavior being monitored. Instead of making futile efforts to stop being controlled and monitored, we should make efforts to ensure that we are being controlled and monitored for the right reasons.<o:p></o:p></span></p>dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-83282097186094215652022-01-30T17:08:00.004-08:002022-01-30T17:13:53.176-08:00Ethics and AI: Is it acceptable to exploit AI or robots and force them into servitude?<p><span style="font-family: arial; text-align: justify;"><span face="Calibri, sans-serif" style="color: #0e101a; font-size: small;">I believe it is acceptable and even desirable to use AI or robots in ways and magnitude that fringe into servitude. I do not agree with the notion of comparing using an inanimate technology to exploitation. I also don’t think AI and robots are being forced into serving humans because as of now, they do not possess any other desires that can be forcibly suppressed in </span><span face="Calibri, sans-serif" style="color: #0e101a;"><span style="caret-color: rgb(14, 16, 26);">favour</span></span><span face="Calibri, sans-serif" style="color: #0e101a; font-size: small;"> of servitude for humans.</span></span></p><p class="MsoNormal" style="font-size: 11pt; line-height: normal; margin: 0cm; text-align: justify;"><span style="font-family: arial;"><b><i><span lang="EN-US" style="color: #0e101a; font-size: 12pt;">Although we fear the lack of human contact that could result from being surrounded by robots, for some tasks, many would prefer machines rather than humans</span></i></b><b><span lang="EN-US" style="color: #0e101a; font-size: 12pt;">.</span></b><span lang="EN-US" style="color: #0e101a; font-size: 12pt;"> This to me captures the purpose and motivation behind having AI or robots perfectly. AI was never imagined to be something that should enjoy the same fundamental rights as the living beings on this planet do. It was conceptualized as a tool to do things for us in an efficient and intelligent manner. Any AI technology should be treated like the resource it is and not as a mortal being. And as is the case with the consumption of any resource, we should be responsible and careful so as to not run it into scarcity or extinction. For me, the question is not whether AI or robots are being forcibly exploited into our service but it is whether or not we are enjoying the services of these ever-improving tools sustainably.<o:p></o:p></span></span></p><p class="MsoNormal" style="font-size: 11pt; line-height: normal; margin: 0cm; text-align: justify;"><span lang="EN-US" style="color: #0e101a; font-family: arial; font-size: 12pt;">The important thing to always keep in mind when thinking about AI is that it is fundamentally different from any form of life on the planet. There may be increasing efforts in making robots like humans but it is hard to see how these robots can be induced with human consciousness. There has been significant support for the argument that <b><i>consciousness can only arise and exist in biological matter</i></b>. And therefore, it is hard to imagine AI robots having human emotions. My point is that an entity being forced into servitude is dangerous if it is capable of damage and has the ability to feel vengeful or angry. While there is no denying the fact that AI can very well inflict great damage to mankind as well as this planet but I doubt that it will do it because of how it was treated unfairly by its “masters”. If the act of using AI relentlessly does not by itself warrant any retaliation then I don’t see any reason to act restrained in this regard.<o:p></o:p></span></p><p class="MsoNormal" style="font-size: 11pt; line-height: normal; margin: 0cm; text-align: justify;"><span style="font-family: arial;"><b><span lang="EN-US" style="color: #0e101a; font-size: 12pt;">Singularity</span></b><span lang="EN-US" style="color: #0e101a; font-size: 12pt;"> or <b>The Intelligence Explosion</b> are popular notions in both the world of scientific research and pop-culture. There is a myriad of arguments that argue against the possibility of a singularity like <b>The Anthropocentric Argument</b> (surpassing human intellect is not some milestone beyond which anything is possible), <b>The Metaintelligence Argument</b> (emphasizing that the capability of doing a task well does not necessarily lead to an improvement in this capability) and <b>The Limits of Intelligence Argument</b> (questioning if there is a limit to intelligence in any form). It is unclear or even doubtful that singularity is a possibility and even if it is possible, there are arguments that it will not directly and immediately imply catastrophe. But even if we assume what science fiction has always offered to be true and believe that singularity means disaster for/ extinction of the human race, then doesn’t it mean that our future is sealed regardless of how we treat AI now? What difference will our current behavior make to a dystopian future where AI will deem humans as a non-necessity and wipe us from the face of the earth?<o:p></o:p></span></span></p><p class="MsoNormal" style="font-size: 11pt; line-height: normal; margin: 0cm; text-align: justify;"><span lang="EN-US" style="color: #0e101a; font-size: 12pt;"><span style="font-family: arial;">In conclusion, I think AI and robots are built to serve humans and there is nothing wrong with being strict in our use of AI as long as we are responsible for the sustainable consumption of this and other resources on our planet. There are many considerations before assuming that AI will eventually become supreme and render our species obsolete. But even if such assumptions are to be made out of extreme caution, I think such destruction is contingent upon how we develop artificial intelligence or how we use it and not how severely we force it into servitude.</span><span style="font-family: Calibri, sans-serif;"><o:p></o:p></span></span></p>dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-4668984139735667212022-01-16T18:17:00.003-08:002022-02-01T19:43:46.732-08:00Long time no see.<div style="text-align: justify;"><span style="font-family: verdana;">I don't know how many month have passed since I have posted something here but I know it's been too many. </span><span style="font-family: verdana;">By all accounts this blog has been nothing more than a dishonest journal of my </span><span style="font-family: verdana;">personality and perhaps it has been nothing less than the best I have achieved. Now that says a lot about my life but more importantly about this blog. </span><span style="font-family: verdana;">I came here today to take a backup of this large pile naivety and I stayed to stack up some more. </span><span style="font-family: verdana;">Some forms of therapy can seem pointless or inconsequential or both. I say that while writing without any seemingly conceivable point knowing quite well that none of this matters. And yet I sense myself healing.</span></div>dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-41573401302426788192017-11-30T03:01:00.000-08:002017-11-30T03:02:38.507-08:00Purging Outlook Account<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
There are two things I don't like about Microsoft Outlook. First is logging in to it and second is logging out. Log in is just a time consuming process that irritates for sure but does bring results later rather than sooner.</div>
<div style="text-align: justify;">
But logging out is a different ball game. Not only is your calmness challenged by the astoundingly long process of actually finding a sign out button, your hopes are crushed too by the inability of that button to actually sign you out. Nothing works, nothing ever works.</div>
<div style="text-align: justify;">
But after struggling for many crucial minutes I finally found a deadly looking solution that worked for me. It worked wonders. It worked magically. It signed me out of Outlook.</div>
<div style="text-align: justify;">
Here it is, put it in a frame if you must, I am putting it in my beloved blog:</div>
<div style="text-align: justify;">
<br /></div>
<div>
Control Panel --> Mail(32 bit) --> Show Profiles --> Delete all profiles.</div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-10401151192862121512016-12-16T01:41:00.000-08:002016-12-30T01:57:50.379-08:00Enabling UART on Raspberry Pi<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">Today I enabled UART on raspberry Pi. The process followed for this can be found at:</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">This was required in order for the ST NFC driver to work on RPI3. Obviously the idea was to use the UART driver in order to test stuff.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-66342748598480061352016-12-15T02:45:00.000-08:002016-12-30T02:51:25.929-08:00Making pi upload pics on bootup<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">Following the task from this week, I had to combine almost everything I did into the ultimate task. The goal was to make pi start taking pictures just as it boots up and then upload them consecutively to a web server. Of course in order to upload pictures the pi must be connected to the internet. And the script to take pictures and upload them should also be working. Since all of these things were working individually, it was time to put all of this together.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">One would think that making pi take pictures on boot up is easy as all that is needed to done is call the script from rc.local and things would fall into place. So I thought. But that was not the case. As it turns out that writing commands in the rc.local only ensures that the commands are run. It does not ensure that the commands are run successfully. This of course was problematic as I had no way to ensure the network was up before pi could start clicking pictures. The whole thing was a synchronization mess. I need to find a better and more clean way of doing this.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-15615160641879713142016-12-15T01:45:00.000-08:002016-12-30T01:59:44.034-08:00Applying driver patch for NFCST<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">Today I build the environment using Buildroot for ST NFC after applying the patch prepared to Buildroot. There were two ways to do this. The first one was the straight forward one, where I would take a Linux source, apply the patch and then have buildroot compile it. But researching a bit I found out that there is way in which we can apply custom patches to the Linux kernel downloaded by buildroot. All we need to do is enable that option and give the path to the kernel patch. And so I did.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-71364389020509711342016-12-14T03:11:00.001-08:002016-12-14T03:11:04.250-08:00Structure Packing<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">Run the following piece of C++ code and observe the output:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">#include <iostream></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">struct something {</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"> char c;</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"> int i;</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"> short s;</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">};</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">int main()</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"> std::cout << sizeof(something) << endl;</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">}</span></div>
<div style="text-align: justify;">
<span style="font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Courier New, Courier, monospace; font-size: large;">The output will be 12.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now run the following program:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">#include <iostream></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">struct something {</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"> int i;</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"> short s;</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"> char c;</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">};</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">int main()</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">{</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;"> std::cout << sizeof(something) << endl;</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-family: Courier New, Courier, monospace; font-size: large;">}</span></div>
<div style="text-align: justify;">
<span style="font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Courier New, Courier, monospace; font-size: large;">The output will be 8.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now that you have run both versions of the same program on a computer, run them in your head. Both programs are doing the same thing. They are printing the size of a struct. The struct contains the same type of variable in both cases. And yet the size of the struct comes out to be different. The structs are almost identical with the only difference being the order in which the variables are declared inside the struct. So the moral of the story is that the order in which variables are declared in a struct determine what the size of struct is going to be. But you should ask your self that why in God's good name should the declaration order matter to size, after all the same number and type of variables are used and hence the size of memory they occupy should also be the same. Well clearly this is not the case. To understand why, you need to learn <a href="http://dnivog.blogspot.in/2016/12/data-allignment-in-c.html" target="_blank">this</a> first. </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Data alignment! It was fun wasn't it? A direct consequence of this sort of alignment of data in C (and C++) is the way in which compound data types like structs are given memory. Remember how C likes its variables to be stored at memory addresses that are a multiple of their size? Well C doesn't discriminate, the same rule applies to structs. So if a struct is of size S, then any instance of this struct will be stored at an address that is a multiple of S. This is very intuitive as you already know why this is required. The issue is the fact that since a struct is a compound data type, its size can be any arbitrary number of bytes depending on what variables it holds as members. The whole alignment thing works only because the data types were allowed to have certain convenient sizes i.e. 2, 4, 8 ... This means that if structs are allowed to have any arbitrary size then aligning them according to their size will make arbitrary sense (i.e. it will work well sometimes and sometimes it will seem stupid).</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Hence it is imperative that the compiler do something to make sure that the structs also end up getting convenient sizes i.e. multiples of 4. The rule that C has to ensure this is rather simple, The size of the struct is always made to be a multiple of the size of the biggest member of the struct. In the example above, the <b>struct something</b> has <b>int i</b> as its biggest member with size 4, so the struct size will also be a multiple of that. How does the compiler achieve this? By using <b>structure packing</b>.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">To understand this, you need to be introduced with some nomenclature. <b>X</b> from now on is the size of the biggest member of the struct. <b>Boundary</b> refers to any multiple of X following the address where the struct memory begins. So some struct object begins its memory at 20 and X is 8 for that struct, then the boundaries are: 28, 36, 44, and so on.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Structure packing is a technique (or a policy) in which if adding a new member of the structure to memory crosses any boundary in the struct, then before adding that member, the leading (proceeding) bits are left as padding and the new member is made to start immediately after the said boundary ends. Finally if after allocating memory to all members in this way, the struct is shy of the next boundary by a few bytes, then those bytes are also used as padding just to make the structure a direct multiple of X and end always end up on the boundary exactly.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Consider the first example of the struct something. The value of X is 4 (size of int i, the biggest member of the struct). Adding the first member of the struct, char c crosses no boundaries and the size of struct reaches 1 (which is the size of char). Then comes the turn of int i. int i is 4 bytes in size, adding it to the memory of struct will make the sum 5. This is problematic, int i is crossing the boundary of the first multiple of 4 i.e. 4. In this case the compiler will decide to allocate 4 bytes to int i starting from byte number 5 only and the bytes in between (2, 3, 4) will be used as padding. So int i will start from 5 and will go till 8. The variable left to be added now is short s which is 2 bytes in size. Adding it to 8 does not cross any boundaries so no proceeding padding is needed. But since the final outcome comes at the total of 10, an extra two bytes are padded just to make the size be 12 i.e. a multiple of 4. This way the final size comes out to be 12 bytes, out which 7 bytes are the variables and 5 bytes are just padding. That is some extravagant wastage don't you think?</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now moving onto the second example. The value of X is still 4. The first variable to be added is int i, adding it gets the total to 4. This covers the first boundary entirely. Then comes short s. Adding this takes the total to 6. So far no addition has cost the total to cross any boundaries. Lastly it is required to add char c. c is 1 byte in size. Adding it to the struct makes the total memory equal to 7, which as you may notice still does not cross the next multiple of 4 i.e. 8. Hence so far we are golden, no padding is needed. The only thing left to do is to add one byte of padding in the end to take the total to an even 8. And vola, that is the final size of this struct, with 7 bytes of actual data and a frugal byte for padding.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">You see now, how rearranging members can make a difference in size of the struct. One might say that okay saving a few bytes is cool but it isn't very significant. That may be true for moderate size programs on moderate size computers, but if either the program is very big or the computer on which it is to be run is low on memory, then these little alterations can add up to amazing benefits.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The next time you are writing a struct, may be keep in mind to arrange the members in such a way that the small ones come together so that they can be fit inside a single chunk and padding is needed to the least.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Another important thing to know here is that the standard does not guarantee that the padded bytes are to be zeroed, they can be anything, from zero to any arbitrary value. It is the programmer's duty to play around with these carefully. Assuming that the members in a struct are all packed together continuously is plain foolish.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-34099953248498334702016-12-14T02:41:00.000-08:002016-12-30T02:45:10.953-08:00Testing picture taking scripts on Pi<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">Raspberry Pi has a pretty cute way of integrating a camera into it. The command to do this is called raspi-still. My task for today was to test a script that took pictures continuously and uploaded them to a web server. The script was ready but it had some bugs as it was not working as expected. I could not tell what the bug is as I was encountering such things for the first time. But after searching on the Internet about how each command written in the script worked, I came to form a decent understanding of how the script worked. This put me in a position change the script around and find the bug. And so I did.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">I had to add some commands in order to make the script fool proof. The script worked like a charm after that.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-62715402291940663352016-12-14T01:46:00.000-08:002016-12-30T02:00:07.664-08:00Applying STNFC driver patch on Linux source<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">In order to test whether the Patch created for the STNFC driver works, I cloned a fresh source for the Linux kernel from github and then applied the patch to it using the git apply command. And then verified if the changes made by the patch were accurate and apt. After solving many issues I was able to create a patch that could singularly make all the changes required by the STNFC driver to work to the Linux kernel</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-89069255521337136082016-12-14T00:57:00.001-08:002016-12-14T00:57:30.533-08:00Data allignment in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
</div>
<div style="text-align: justify;">
<span style="font-size: large;">I am fan of travelling. But just not as a hobby or an activity I'd like to take, instead as an analogy to understand and hence communicate some efficiency concerns in computers. I am going to do this again.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">When one has to travel and that to frequently then what you'd expect the number of items of their luggage to be? As less as possible right? A traveller (at least a wise one) would want to pack things in (say) one bag so that carrying stuff isn't as prominent a pain as it otherwise would be. Now assume that on a single trip, one is allowed to take only one bag. Considering the trip costs money and more importantly, time, one would want to take this trip once only. Packing your stuff in two separate bags would be stupid as then you'd have to travel twice in order to complete the transfer.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The idea is to keep things packed in such a manner that one would not require more bags to carry them. You want only one bag or at the most, a number of bags that you can carry at once.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">A program travels too, more frequently then any human in any imaginable circumstance. And while travelling, a program also has to carry stuff from memory to the CPU, or back. As such you'd want your program to carry the whole data belonging to the same variable once, in one data cycle. A 64 bit CPU can carry 64 bits at once. So if you have 65 bits of data in memory then that will force your CPU to take two trips in order to fetch this data entirely. But this is okay as no altercation would make this transaction more efficient, CPU can only carry 64 bits in parallel.</span><br />
<span style="font-size: large;">Another interesting peculiar feature of CPUs is their habit of considering only those chunks of memory that start from an address which is a multiple of the word size. So if the word size was X then the CPU would only fetch memory chunks starting from 0, X, 2X, 3X, ... This means that if some thing is placed in such a way in memory that it is not wholly a part of any of these chunks, then the CPU will have to make two trips in order to fetch it and then will have to perform some bit shifting in order to restore the proper value of the thing being fetched as well.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Data types in C (with exception of char) have even bit sizes. Their size is always a multiple of 2. More importantly, except for chars, all other primitive data types in C are either a multiple or a factor of the word size on most architectures. Most modern architectures have a word size which is a multiple of 4, so this means that almost all primitive data types in C should also be a multiple or a factor of 4 bytes in size. You won't see a data type in C that is primitive and has a size of 6 bytes or 10 bytes or 14 bytes with the exception of long double. The size will be either 2, 4 or any multiple of 4.</span><br />
<span style="font-size: large;">When a compiler has to store a variable of a particular type, the starting address available to store the variable can be anything (depending what and how things were packed before). If the compiler just went ahead and stored the variable exactly at the address that was available then that could lead to some inefficiencies in getting that variable back from memory. For example consider that the variable size is 4 and the starting address available to store the variable is 18, then if the storage was done simply, the variable will reside in memory from the address 18 to 22. This will lead to the same problem described in the earlier paragraphs. Since the variable will be residing in two separate memory chunks (that start from a multiple of 4 and are 4 bits in size), the CPU will have to take two trips in order to fetch it completely and then will have to perform some bit shifting on the partial values of both the chunks in order to retrieve the proper value of the variable. Now ask yourself, wouldn't it be nicer if this variable was stored at address 20 to 24 even if that meant leaving two empty bits at 18 and 19? Of course it would, as now the CPU could simple fetch the variable in one go and would not require any manipulation of bits in order to get the proper value of the variable.</span><br />
<span style="font-size: large;">For this reason the C language enforces some rules that determine how the variables of various sizes are to be stored. The general rule is that if a variable is of size S then it must be stored at an address that is a multiple of S as well. Since the the size of variables (as mentioned above) align (mostly) with the word size of machines, this enforcement ensures that no variable has to be split between two memory words.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-43329494720539762972016-12-13T01:48:00.000-08:002016-12-30T02:00:24.793-08:00Git send email for STNFC<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">I had studied and used git send email before but had left it there only. Today I was required to use it in order to create a patch for STNFC driver. This made many changes to the Linux kernel so the size of the patch I was required to create was clearly bigger than any patch I have ever created.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">I needed to brush my skills over git send-email in order to be able to do this. It helped that when I studied it I create a document explaining everything required. I studied up that document and went ahead with the work in hand.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">First of all I made all the changes by hand. Commited the changes and then created a patch from it. But of course the patch created was not in the right format as checked by the scripts given in the Linux kernel source. So I had to add signed off to the commit and also a description of the commit in order to make things perfect. Finally I had a single patch that could hopefully incorporate all the changes required to run the driver.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-33376476284908948242016-12-12T01:53:00.000-08:002016-12-30T02:00:37.394-08:00Configuring kernel for ST NFC support<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: "verdana" , sans-serif; font-size: large;">Making a driver work in the kernel is not just about having to add the source and compile the kernel with it. The driver may depend on a lot of things in order to work properly. And all of these things need to be enabled. Same was the case with the STNFC driver. First of all it used to transport buses (not together of course), SPI and UART. So in order to use them, proper support was required in the kernel source.</span></div>
<div style="text-align: justify;">
<span style="font-family: "verdana" , sans-serif; font-size: large;">Today I spend the whole day figuring out how to enable these and what exactly is required for the driver to work. Finally I was able to enable all the necessary things.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-58781111509006410012016-12-10T06:51:00.002-08:002017-02-04T01:06:32.972-08:00Spartan Programming<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">You all have seen the movie <b>300</b> which makes you enough of an expert to understand this post. It would help if you knew some programming as well but it's just an after thought so don't beat yourself for it.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Remember how Spartans are almost anti-fancy in their attire and yet a menacing force of nature? Well it so happens that there is a culture in programming inspired almost from this principle i.e. be as effective as possible without using too many tools. This culture is aptly named Spartan programming.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">A programming work can be deemed as being Spartan based on the following criteria:</span></div>
<br />
<ol style="text-align: left;">
<li style="text-align: justify;"><span style="font-size: large;"><b>Horizontal complexity</b>: A program shouldn't be too nested. More nesting there is, the more chances you have of clearing your logic and making it more streamlined.</span></li>
<li style="text-align: justify;"><span style="font-size: large;"><b>Vertical complexity</b>: The length of the program should be as short as possible. No point in writing more code when the problem can be solved with something shorter and hence sweeter.</span></li>
<li style="text-align: justify;"><span style="font-size: large;"><b>Token count</b>: A token is any entity in a program that have individual meaning be it your variables, literals, keywords or anything that is allowed by the language and is part of your program. It is obvious that reducing token count without loosing meaning can ensure simplicity to your algorithm and hence can be more effective and scale-able.</span></li>
<li style="text-align: justify;"><span style="font-size: large;"><b>Character count</b>: Don't be too verbose with your comments and variable names. Be brief and to the point.</span></li>
<li style="text-align: justify;"><span style="font-size: large;"><b>Parameters</b>: It is always nice to refactor a solution into subroutines. A good subroutine should have a clear purpose and should be spartan in itself. One of the essential elements of ensuring this would be reducing the passing of implicit data to subroutines as arguments. You don't want to pass things that are available otherwise and can be derived from other parameters.</span></li>
<li style="text-align: justify;"><span style="font-size: large;"><b>Variables</b>: Don't overuse a variable in too many places. It is also considered a good practice to declare variables immediately before they are to be used and not all together at the top of your program.</span></li>
</ol>
<div style="text-align: justify;">
<span style="font-size: large;">In conclusion, spartan programming encourages frugal use of resources as a programmer. Another important feature is to make your solution robust just like a spartan. A spartan isn't invincible but it certainly should be tough enough.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-17577946533587553142016-12-10T01:40:00.002-08:002016-12-10T01:40:56.866-08:00Programming is about dumbing things down.<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">The more code you write, the more you wish you had written less.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">What I me</span><span style="font-size: large;">an by this is that after completing almost any programming assignment, one looks at ways in which the code could be optimized. Since optimization is a much more intellectual task, programmers like me try to confuse it with the size of code. I belong to a category of coders that think less the code more optimized the algorithm is. And you know what, for once I think I am on the right side of things in computing.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">While being able to write crazy amount of code is a fantasy we all have but no one would question the beauty of an elegant solution to a tricky problem that saved day at the office. Computer scientists make a living out of telling dumb things (computers) to do dumb things (instructions) in order to achieve impressive feats (solve a Rubik's cube or simulate the universe, take a pick). What this process does is that it eventually breaks in a computer scientist the egoistic complexity a human has innately. They (I hesitate to include myself) tend to think about things from a more elementary point of view. Redefining problems in order to make the solution seem much more obvious, classifying behavior in order to be able to compute it and being succinct in what they have to offer for the sake of being fast and not more omniscience.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">An awesome algorithm may not be one that solves a hundred problems at once but rather an algorithm that solves a single problem simply. It is important for a coder to realize that computers can never be as smart as we are. Getting smart requires evolution and evolution takes time. So in order to be effective as a programmer one needs to learn to solve problems from a much more dumber perspective. Once we move being intelligent out of the equation we can start focusing on being less complicated. And once we are less complicated, we are less inclined to be wrong. And what do we call something that is not wrong? Right.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Write programs that can make sense to computers, after all they are the ones that have to run it.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-76384467410167489242016-12-10T01:11:00.001-08:002016-12-10T01:11:53.718-08:00UML: User Mode Linux<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">Linux is a name for a kernel. A kernel is a big piece of tough software that makes the wilderness of hardware behave for the comfort of the user. The user may run applications of varying tastes on the hardware without ever having to explain the billion gates inside of a computer, which way to let the current follow and to hold on which side. It is all taken care of the kernel.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">But what if I told you that the kernel too can be run as an application. After all it is in the end a piece of software although a little firm. This idea isn't that alien of course. It kind of resembles virtualization.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">You see, Linux being a kernel is meant to work on and with hardware. But if one wishes to run Linux in user mode i.e. like an application then it can't be given the kind of access it needs to the hardware. So what is the best that can done? Visualize an entire machine of hardware in software (simulate) and allow the application (Linux) to run the courses on it. It is important to note that the host for such activities must also be running on Linux.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">It is an immediate given that this is fun. Running multiple copies of a kernel on machine simulates having multiple machines working independently of each other on a single big piece of hardware. Apart from being funny, this can also be very useful. The idea of having a kernel but not with unlimited power and almost no authority over your not so free and hence obviously precious hardware makes perfect sense for innovative development and kernel tweaking. You can use this technique to alter the kernel as much as you like without putting your computer in harm's way.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Even if you have some sincere activities planned, you can seek great aid from such virtualization. For example multiple servers can be hosted from a single machine this way. It can also be a great way to teach students about kernels and Linux.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">All things said, this technique is not yet comprehensive. There are many leaps of development needed before it becomes more popular and hence starts gaining some developer attention as well.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com1tag:blogger.com,1999:blog-2349201029114250817.post-42886071223070331852016-12-09T03:32:00.002-08:002016-12-09T03:32:54.635-08:00Let's ID UUIDs<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">A UUID stands for Universally Unique Identity. Pretty heavy don't you think? To claim that something is unique in the entire universe is a bold move. Anyway, a UUID is a number representing the type of data stored in BLE servers, beacons and anything that uses Bluetooth and stores some data.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The way Bluetooth technology looks at data is centred around UUIDs. A UUID is used to classify data into certain types. It is just like a data type like your ints and floats of the world. The difference is that each type is a huge number and there is a huge number of these types.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Like in any programming language, same binary digits would mean different things depending on what type of data they are i.e. what is their UUID.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now that you know what a UUID is, I want you to question who decides these UUIDs? The obvious answer would be the people that came up with the whole idea of UUIDs i.e. the Bluetooth SIG. This answer is true but not complete. As it turns out, there are two types of UUIDs that can be found:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">One type is of the UUIDs that are <b>16 bits</b> long and the other is of the UUIDs that are <b>16 bytes</b> long. So you can imagine that the first type will contain a much lesser number of UUIDs.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The 16 bit UUIDs are provided by the standard. They were set at the time of inception of this specification or in consecutive years but only the SIG. A list of these UUIDs can be found at the Bluetooth website. Since these are provided by SIG, they remain constant. These are sort of like the built in data types supplied by a programming language.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The 16 byte UUIDs are kind of made up by various organisations and companies around the globe (or dare I say the universe) as they go along exploring newer areas and inventing new kinds of data crunched by Bluetooth devices. Once someone has an idea for a new type of data (a new profile), then they have to register that with the SIG and also provide the specification for it. This type will then be given a 16 byte unique number which will not be used by any other type afterwards. The larger length of this type of UUID make sense because the number of such applicants is enormously big. This whole thing is like making a new class and then turning it into a library for the rest of the world to use. The UUID is just a unique ID for your class/library.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-35807884279583113562016-12-09T03:11:00.001-08:002016-12-09T03:33:51.937-08:00Advertising using a BLE beacon in Linux<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">Beacons are a great way to send brief but important data to nearby scanning objects. They will be a force to be reckoned with in the future i.e. a world full of Internet of Things. In this post I will present a way in which you can advertise like a beacon from a Linux machine with attached or inbuilt Bluetooth low energy hardware. </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Since one expects beacons to advertise constantly, it is more likely that you'll be applying this concept to less consuming computers like your Raspberry Pi's or BeagleBone Black's. </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Let us get on with some primary installation stuff. Most of these things should already be installed on most systems but run these commands just for the sake of it:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo apt-get install libusb-dev </i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo apt-get install libdbus-1-dev </i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo apt-get install libglib2.0-dev --fix-missing</i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo apt-get install libudev-dev </i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo apt-get install libical-dev</i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo apt-get install libreadline-dev</i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo apt-get install xclip</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">After this, you need to <a href="http://dnivog.blogspot.in/2016/12/finding-enabling-and-starting-bluetooth.html" target="_blank">start your Bluetooth adaptor</a> (assuming it is BLE supportive, find all about this <a href="http://dnivog.blogspot.in/2016/12/checking-availability-of-ble-in-linux.html" target="_blank">here</a>). Now lets get to business.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">A beacons advertises. You know that. But the next defining thing about beacons is their inability to connect to any scanning device. A beacon just transmits data, it does not engage in a connection with any other BLE device. So the first thing we need to do is get your adaptor advertising in <b>non-connectable</b> mode:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo hciconfig hci0 leadv 3</i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo hciconfig hci0 noscan</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The number 3 in the above command represents the non-connectable state. To advertise in connectable state use 0 instead and loose the successive "noscan" command.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Your adaptor should be advertising now. Now it is time to put some data in the advertisement. The data to be advertised has to be classified into a type. The beacon must also tell what type of data it is advertising. After all the thing to be advertised will be binary digits only but the way these bits are to be interpreted by the scanner depends on the type of data the bits represent.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The type of data in BLE specs is governed by the <b>UUID</b> which stands for Universally Unique ID. UUIDs are of different types (learn more about them <a href="http://dnivog.blogspot.in/2016/12/lets-id-uuids.html" target="_blank">here</a>). The type of UUID we are going to use is a 16 byte UUID.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Since the UUID is 16 bytes long, it can practically be any random number. So go ahead make up any 16 bytes of imaginary data, write them up in hex like so:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8 63 B5</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The command used for sending out data is</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo hcitool -i hci0 cmd 0x08 0x0008</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">But it takes a few arguments, one of which is the UUID that we have just covered, now lets look at the rest of the arguments.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The first byte in the argument list is the length of the entire packet to be sent. Lets just assume it to be <b>XX</b> for now, once we have figured out the entire packet then we can come back and fill in the length accordingly.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The rest of the packet is made of structures called <b>AD structures</b>. An advertising packet basically contains, the length field (XX for now) followed by any arbitrary number of AD structures each having a special purpose. An AD structure has the following format:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">1 byte for the length of the structure following the length byte (assume this to be n)</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">1 byte for the type of the AD structure. There are certain types of these structures, each type representing a purpose.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">n -1 bytes of data.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Our packet is going to have 2 AD structures.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The first AD structure is to set certain flags. Such AD structures that are used configure flags have the type <b>01</b>. The value of the structure is the flag mask to be used to configure the flags. The flag mask is only 1 byte long and I am not even going to pretend that I know anything about what these flags mean. You're just going to have to trust me with the value and the value is <b>1A</b>. So in total the length should be <b>02</b> as that is the number of bytes in this AD structure after the first length byte.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The next and last AD structure will eventually contain the payload of this packet which constitutes the value and UUID. We will calculate the length after all the other bytes have been accounted for in this structure. The type of this AD structure is <b>FF</b>. It represents <b>Manufacturer information</b>. The value of this AD structure contains the company ID followed by the payload. The company ID is a two byte code assigned by Bluetooth SIG to top semiconductor industries in the world. The code for <b>STMicroelectronics</b> for example is <b>00 30</b> (to be written in the reverse order in the packet). After the company code, there is a byte that represents the number of bytes remaining from here in out in this AD structure. The value for this is <b>15</b> in hex which equals to 21 more bytes. 16 of these 21 bytes are the UUID. Following the UUID is your 4 byte data which can be anything of your choice (I am going to use <b>00 00 00 00</b>). The last byte in the structure is the <b>Tx</b> power with which this signal is to be transmitted.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Let me put this in order and then we can count the number of bytes and substitute that for XX.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i><span style="background-color: #cccccc;">XX</span> <span style="background-color: #9fc5e8;">02 01 1A</span> <span style="background-color: #cccccc;">1A FF</span><span style="background-color: white;"> </span><span style="background-color: #cccccc;">30 00</span> <span style="background-color: #9fc5e8;">02 15</span> <span style="background-color: #cccccc;">63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8 63 B5 </span><span style="background-color: #9fc5e8;">00 00 00 00</span> <span style="background-color: #cccccc;">C8</span></i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">So these are 30 bytes following one byte for length. Hence the first byte should be the hex representation for 30 i.e. <b>1E</b>. Now, finally our command looks like this:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 30 00 02 15 63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8 63 B5 00 00 00 00 C8</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">You can now scan the beacon from other BLE devices. I would recommend using the beacon scanner app for Android.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">To stop advertising use the following command:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">sudo hciconfig hci0 noleadv</i></span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com1tag:blogger.com,1999:blog-2349201029114250817.post-48965929562703603892016-12-09T02:51:00.000-08:002016-12-30T03:01:47.612-08:00Making pi upload pics after bootup<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: "verdana" , sans-serif; font-size: large;">After the ordeal from yesterday I started out the day with a fresh mind looking for a fresh approach to the task at hand. After some digging around the web I found that there ways in which we can trigger certain scripts in Linux when links are up. The 3g dongle creates a ppp link. All that is needed to place the script after giving it proper permissions at a particular place under if-up.d/ directory of the kind of protocol used in the link (in our case it is ppp). So I did and the results are positive but not consistently. As it turned out my script had some bugs still. I fixed them, it took me the whole day and also logged the output into a file.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-66246447015201211352016-12-06T02:35:00.000-08:002016-12-30T02:41:08.018-08:00Automatic connection on Bootup with Pi<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">One of the things that make Raspberry Pi so popular is the fact that it is almost a computer that can do almost anything a normal computer can do for a person but at a very low energy consumption. This means that one can have Pi running 24x7 and not be affected by how much it would cost. If a pi is running non stop it only makes sense that it is at some remote place and as such it must be connected to the Internet otherwise how else will it be able to communicate with any other computer. So one of the important things is to make your pi connect to the internet automatically when it boots up. This is useful when for some reasons your pi has to be rebooted and you want it to become active on the network without you being there. This is exactly what I wished to do with a pi today.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">The internet connection was to be made through a 3g dongle and using the sakis3g software. In order to connected to the dongle, a single command is to be run. This makes the whole thing very easy. All that is needed is to make sure that this command is run every time pi boots up.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif; font-size: large;">One of easiest ways to do this is to have that command written inside the /etc/rc.local script. So I did and it worked.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-7201417504609916552016-12-06T01:29:00.002-08:002016-12-06T01:29:52.592-08:00Checking availability of BLE in Linux<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">Most modern PCs have Bluetooth as part of their hardware. But not all of them are Bluetooth Low Energy enabled. I am going to describe a quick way to determine whether your Linux machine is BLE enabled or not. </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">First thing to check is to see if the machine is Bluetooth enabled. To do so, follow this <a href="http://dnivog.blogspot.in/2016/12/finding-enabling-and-starting-bluetooth.html" target="_blank">link</a>.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">After confirming that Bluetooth is there and is running, you are ready to enter into the world of BLE provided of course you're able to.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Open up a terminal and type the following command, assuming hci0 is your adaptor:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span style="font-size: large;">$ hciconfig hci0 lestates</span></i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">This command will return one of two kind of output depending on whether BLE is present or not. If you get something like this:</span></div>
<div style="text-align: justify;">
<i><span style="font-size: large;"><br /></span></i></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span style="font-size: large;">Read LE supported states on hci0 returned status 1</span></i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Then this means that BLE is not present and this is not a limitation you can fix by tweaking software. You need to purchase a Bluetooth Low Energy adaptor and plug it in to your machine. </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">On the spin side, if you see a list of states offered by your machine then that obviously mean BLE is good to go. The output may look something like:</span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><br /></i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i>Supported link layer states:</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Non-connectable Advertising State</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Scannable Advertising State</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Connectable Advertising State</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Directed Advertising State</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Passive Scanning State</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Active Scanning State</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Initiating State/Connection State in Master Role</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Connection State in the Slave Role</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Non-connectable Advertising State and Passive Scanning State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Scannable Advertising State and Passive Scanning State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Connectable Advertising State and Passive Scanning State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Directed Advertising State and Passive Scanning State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Non-connectable Advertising State and Active Scanning State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Scannable Advertising State and Active Scanning State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Connectable Advertising State and Active Scanning State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Directed Advertising State and Active Scanning State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Non-connectable Advertising State and Initiating State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Scannable Advertising State and Initiating State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Non-connectable Advertising State and Master Role combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Scannable Advertising State and Master Role combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Non-connectable Advertising State and Slave Role combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Scannable Advertising State and Slave Role combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Passive Scanning State and Initiating State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Active Scanning State and Initiating State combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Passive Scanning State and Master Role combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Active Scanning State and Master Role combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Passive Scanning State and Slave Role combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Active Scanning State and Slave Role combination</i></span></div>
<div style="text-align: justify;">
<span style="background-color: #cfe2f3;"><i><span class="Apple-tab-span" style="white-space: pre;"> </span>YES Initiating State and Master Role combination/Master Role and Master Role combination</i></span></div>
<div style="text-align: justify;">
<br /></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-71347738431227438532016-12-06T01:18:00.003-08:002016-12-06T01:18:24.570-08:00Receiving files from a phone using Bluetooth on a Linux machine<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">Following the other <a href="http://dnivog.blogspot.in/2016/12/finding-enabling-and-starting-bluetooth.html" target="_blank">posts</a> in this genre, I am going to describe a way to receive files sent from a mobile phone to a Linux machine using Bluetooth and as advertised, everything is going to be directly from the terminal.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">First of all you need to look for a Bluetooth service running on the Linux machine. To do so, list the services out using the following command:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i style="background-color: #eeeeee;"><span style="font-size: large;">$ sudo sdptool browse local | grep "Service Name"</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">This will list out all the Bluetooth services offered presently by the machine. In order to receive files we need the service: "<b>Object Push Profile</b>". If it is listed then all is set otherwise you need to install it by either following <a href="http://www.instructables.com/id/Make-Raspberry-Pi-device-become-a-Bluetooth-Object/step0/Make-Raspberry-Pi-device-become-a-Bluetooth-Object/" target="_blank">this</a> tutorial or simply following these commands:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-size: large;">$ sudo apt-get install obexpushd</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Check if the <b>obex-data</b> process is still running:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-size: large;">$ ps auwx | grep obex-data</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">This will return a process id if the process is running otherwise nothing will be returned. Assuming the process id is XXXX, apply:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-size: large;">$ kill -9 XXXX</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Start the service now using the following command:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc;"><i><span style="font-size: large;">$ sudo obexpushd -B -n</span></i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">It should give the following output:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-size: large;">This software comes with ABSOLUTELY NO WARRANTY.</span></i></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-size: large;"> This is free software, and you are welcome to redistribute it</span></i></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-size: large;"> under certain conditions.</span></i></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-size: large;"> Listening on bluetooth/[00:00:00:00:00:00]:9</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"> </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Keep this thing running and open another terminal. Following the procedure given in <a href="http://dnivog.blogspot.in/2016/12/sending-files-using-bluetooth-from.html" target="_blank">this</a> post, connect with the phone you wish to communicate with.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now it is time to send files from your phone. Your machine will be ready to receive the files by now. When the transfer is done, you can quit the interactive session and kill the obexpushd process by doing <b>ctrl + c</b>.</span></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0tag:blogger.com,1999:blog-2349201029114250817.post-61659396820876476092016-12-06T00:51:00.001-08:002016-12-06T00:51:48.803-08:00Sending files using Bluetooth from terminal to an Android phone<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">In the last <a href="http://dnivog.blogspot.com/" target="_blank">post</a> I described how to get Bluetooth up and running on a Linux machine all from the terminal. In this post I am going to describe one half of the most obvious use case of Bluetooth i.e. sending files from the Linux machine to another Bluetooth device e.g. a phone.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">I am assuming that you have a Bluetooth adaptor running, if no then check the above mentioned post on how to do that. </span></div>
<div style="text-align: justify;">
<span style="font-size: large;">First thing you are going to do has little to do with your Linux machine. You need to make your phone a <b>ftp server</b>. Don't worry, it's not very fancy. An app installation will do just fine. The app I'd recommend for this is the <b>Astro Bluetooth Module</b> by <b>Metago</b>. Once the app is installed, open it and tick the following three things (you are gonna wanna do this every time you need to receive files):</span></div>
<br />
<ul style="text-align: left;">
<li style="text-align: justify;"><b><span style="font-size: large;">Bluetooth</span></b></li>
<li style="text-align: justify;"><b><span style="font-size: large;">Discover-able</span></b></li>
<li style="text-align: justify;"><b><span style="font-size: large;">OBEX ftp server</span></b></li>
</ul>
<div style="text-align: justify;">
<span style="font-size: large;">OBEX is the <b>Ob</b>ject <b>Ex</b>change profile that is designed specially for the transfer of files between two Bluetooth devices.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now it is time to turn to the Linux machine and make sure it is ftp ready. First thing is to just update stuff just for good measure:</span></div>
<div style="text-align: justify;">
<span style="font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;">$ sudo apt-get update</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Then you can go ahead and add <b>obexftp</b>:</span></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></i></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;">$ sudo apt-get install obexftp</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now it is time to hunt for the phone you want to interact with. Use the following command to scan all nearby Bluetooth devices:</span></div>
<div style="text-align: justify;">
<span style="font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;">$ hcitool scan</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">It will start scanning like so:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<br />
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;">Scanning ...</span></i></div>
<div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span class="Apple-tab-span" style="white-space: pre;"> </span>B4:CE:F6:B5:45:D7<span class="Apple-tab-span" style="white-space: pre;"> </span>Nexus 9</span></i></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span class="Apple-tab-span" style="white-space: pre;"> </span>D8:5D:E2:A8:0B:B4<span class="Apple-tab-span" style="white-space: pre;"> </span>DIPTENDU-PC</span></i></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span class="Apple-tab-span" style="white-space: pre;"> </span>5C:E0:C5:56:D3:CF<span class="Apple-tab-span" style="white-space: pre;"> </span>PRIYARAN-E7450</span></i></div>
</div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">You need to copy or at least make a note of the address of the device you wish to communicate with. Lets assume the address of the device is <b>B4:CE:F6:B5:45:D7</b>.</span></div>
<br />
<div style="text-align: justify;">
<span style="font-size: large;">After this you're going to use an interactive tool provided to you already in order to do any interactions using Bluetooth. The tool is <b>bluetoothctl</b> and you can invoke it using the command:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;">$ bluetoothctl</span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Trust me and type the following four commands directly into the interactive session:</span></div>
<div style="text-align: justify;">
<span style="font-family: Courier New, Courier, monospace; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span style="background-color: #b4a7d6;">[bluetooth]#</span> <span style="background-color: #cccccc;">power on</span></span></i></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span style="background-color: #b4a7d6;">[bluetooth]#</span> <span style="background-color: #cccccc;">agent on</span></span></i></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span style="background-color: #b4a7d6;">[bluetooth]#</span> <span style="background-color: #cccccc;">default-agent</span></span></i></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span style="background-color: #b4a7d6;">[bluetooth]#</span> <span style="background-color: #cccccc;">scan on</span></span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Now connect to the device using its MAC address:</span></div>
<div style="text-align: justify;">
<i><span style="font-size: large;"><br /></span></i></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span style="background-color: #b4a7d6;">[bluetooth]#</span> <span style="background-color: #cccccc;">pair B4:CE:F6:B5:45:D7</span></span></i></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span style="background-color: #b4a7d6;">[bluetooth]#</span> <span style="background-color: #cccccc;">trust B4:CE:F6:B5:45:D7</span></span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Obviously you'll get some kind of verification code on your phone to confirm this connection, accept the invitation. You have now paired with the phone. Without closing this terminal, open up another one. And type the following command:</span></div>
<div style="text-align: justify;">
<i><span style="font-size: large;"><br /></span></i></div>
<div style="text-align: justify;">
<i style="background-color: #cccccc;"><span style="font-family: Courier New, Courier, monospace; font-size: large;">$ obexftp -b B4:CE:F6:B5:45:D7 -p <b>/path/to/file/to/be/send</b></span></i></div>
<div style="text-align: justify;">
<b><span style="font-size: large;"><br /></span></b></div>
<div style="text-align: justify;">
<span style="font-size: large;">From here you should get a prompt at your phone showing the transfer happening.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Once the file transfer is done, exit the terminal window and quite the interactive session by typing:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span style="background-color: #b4a7d6;">[bluetooth]#</span> <span style="background-color: #cccccc;">quit</span></span></i></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">To know more about these interactive commands, you can use </span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<i><span style="font-family: Courier New, Courier, monospace; font-size: large;"><span style="background-color: #b4a7d6;">[bluetooth]#</span> <span style="background-color: #cccccc;">help</span></span></i></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com1tag:blogger.com,1999:blog-2349201029114250817.post-40638934424702965182016-12-06T00:16:00.001-08:002016-12-06T00:52:09.830-08:00Finding, enabling and starting Bluetooth in Linux from terminal<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<span style="font-size: large;">First thing that is needed to be done is to check if your machine has a Bluetooth adaptor or not. Most modern PC should have one builtin but if that is not the case one can always buy a cheap Bluetooth USB dongle and plug it in. Once the hardware side of things are done with, enter the following command to start the essential software routines to handle Bluetooth:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i>$<span style="background-color: #cccccc;"> sudo service bluetooth start</span></i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">To know what and how many adaptors you have at your disposal you need to use the following command:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">$ hciconfig</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">The above command will return nothing if no Bluetooth adaptor is available, else you will see something like this as output:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">hci0:<span class="Apple-tab-span" style="white-space: pre;"> </span>Type: BR/EDR Bus: UART</i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;"><span class="Apple-tab-span" style="white-space: pre;"> </span>BD Address: B8:27:EB:E4:87:C3 ACL MTU: 1021:8 SCO MTU: 64:1</i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;"><span class="Apple-tab-span" style="white-space: pre;"> </span>UP RUNNING</i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;"><span class="Apple-tab-span" style="white-space: pre;"> </span>RX bytes:773 acl:0 sco:0 events:50 errors:0</i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;"><span class="Apple-tab-span" style="white-space: pre;"> </span>TX bytes:2540 acl:0 sco:0 commands:50 errors:0</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">What you see above is the primary information about the adaptor <b>hci0</b>. The zero here is like a serial number, if the system had two adaptors then there would have been two such outputs following hci0 and hci1. For the rest of the commands in this post I am going to use hci0 as a default.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;">If you wish to know more about the adaptor then use:</span></div>
<div style="text-align: justify;">
<span style="background-color: #cccccc; font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">$ hciconfig -a hci0</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">In the output of the above commands you see in the second line something written as "<b>UP RUNNING</b>". This represents the current state of the adaptor. Currently hci0 is running. To shut the radio down use:</span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i><br /></i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">$ hciconfig hci0 down</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">To bring it back again:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">$ hciconfig hci0 up</i></span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">That is it. Bluetooth is now functional. As a validation you can scan using other devices to see if your device gets listed there. It should.</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-size: large;">Once you're able to get this and are willing to continue using Bluetooth from the terminal, you need to install the following things for a better experience:</span></div>
<div style="text-align: justify;">
<span style="font-size: large;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">$ sudo apt-get install <b>bluez</b></i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;">$ sudo apt-get install <b>bluez-tools</b></i></span></div>
<div style="text-align: justify;">
<span style="font-family: "courier new" , "courier" , monospace; font-size: large;"><i style="background-color: #cccccc;"><b><br /></b></i></span></div>
<div style="text-align: justify;">
<br /></div>
</div>
dnivoGhttp://www.blogger.com/profile/09050502166299836238noreply@blogger.com0